1

When working with one-to-many relations in with Rails 3.1 and Mongoid, I keep bumping my head on undefined method `name' for nil:NilClass even when I'm positive it exists. Either it's a stupid mistake or then there's something wrong with Mongoid. Let's elaborate:

I keep getting this error:

NoMethodError in Leads#index

Showing /app/views/leads/index.html.haml where line #19 raised:

undefined method `heat' for nil:NilClass
Extracted source (around line #19):

16:       - @leads.each do |lead|
17:   
18:         %tr
19:  %td #{lead.visit.heat}°
20:  %td
21:    = link_to lead.name, :controller => "leads", :action => "show", :id => lead.id

And when I try to reproduce this in the console, it seems to work great. Truly mind-boggling..

Here's the code from relevant places:

-------------------------*SCHNIP*------------------------------------
class Company
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  has_one :visit

  def self.get_companies
    visits = Visit.get_visits

    companies = self.all
    visits.each do |visit|

      unless companies.name.include?(visit.name)
        new_company = self.new 

        new_company.name = visit.name        
        new_company.visit = visit
        new_company.save
      end
    end

    #return companies for current instance
    return Company.where(:visit.exists => true)
  end
end

-------------------------*SCHNIP*------------------------------------

class Visit
  include Mongoid::Document
  include Mongoid::Timestamps

  field :heat, type: Integer
  field :name, type: String

  belongs_to :company


  def self.get_visits
    return self.all
  end

end

-------------------------*SCHNIP*------------------------------------

class LeadsController < ApplicationController
  def index
    @selected = 'visitors'
    @leads = Company.get_companies
  end
end

-------------------------*SCHNIP*------------------------------------

app/views/leads/index.html.haml

- @leads.each do |lead|

  %tr
    %td #{lead.visit.heat}°
    %td
      = link_to lead.name, :controller => "leads", :action => "show", :id => lead.id

-------------------------*SCHNIP*------------------------------------
tshepang
  • 12,111
  • 21
  • 91
  • 136
Herbert
  • 11
  • 2

3 Answers3

1

I just ran into this, I had an Account -> Transaction relationship.

I embedded Transactions into Account, which then prevented me from making Transactions on their own. I got the same error message.

But if I did this:

a = Account.create
a.transactions.create

Then everything went fine. Hope that helps explain something.

stuartc
  • 2,244
  • 2
  • 24
  • 31
0

Not the answer to your question but why do you have:

  def self.get_visits
    return self.all
  end

In your Visit model is this not the same as calling Visit.all ?

When you call lead.name lead is nilclass it not a company as i guess you are expecting it to be.

It all seems a bit odd and far to much code for what you are trying to achieve.

I would go back to basics.

nodrog
  • 3,532
  • 2
  • 25
  • 31
0

The error message you presented actually suggests that the lead in question has a Null lead.visit somewhere. You have a defined "lead", but its "visit" was not defined.

Are you sure that you can use .exists as in :visit.exists? It seems that you are receiving some leads which actually do not have that visit field.

To check, you could try something like

- @leads.each do |lead|
    - if lead.visit
        %tr
            %td #{lead.visit.heat}°
            %td
                = link_to lead.name, :controller => "leads", :action => "show", :id => lead.id

Please check if this works.

FernandoH
  • 855
  • 1
  • 9
  • 17