Mongoid:未定义的方法“名称”对于 nil:NilClass,即使它存在

发布于 2024-12-04 11:31:19 字数 2046 浏览 7 评论 0原文

当使用 Rails 3.1 和 Mongoid 处理一对多关系时,我不断地碰到 nil:NilClass 的未定义方法“name”,即使我确信它存在。要么这是一个愚蠢的错误,要么是 Mongoid 出了问题。让我们详细说明一下:

我不断收到此错误:

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

当我尝试在控制台中重现此错误时,它似乎工作得很好。真的令人难以置信..

这是相关地方的代码:

-------------------------*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*------------------------------------

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*------------------------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹂绝世的画 2024-12-11 11:31:19

我刚刚遇到这个问题,我有一个帐户 ->交易关系。

我将交易嵌入到帐户中,这阻止了我自己进行交易。我收到了同样的错误消息。

但如果我这样做:

a = Account.create
a.transactions.create

那么一切都会顺利。希望这有助于解释一些事情。

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.

青芜 2024-12-11 11:31:19

不是您问题的答案,但您为什么会这样:

  def self.get_visits
    return self.all
  end

在您的 Visit 模型中,这与调用 Visit.all 不一样吗?

当您致电 Lead.name Lead is nilclass 时,它不是一家公司,正如我猜您所期望的那样。

这一切看起来有点奇怪,而且对于您想要实现的目标来说,代码太多了。

我会回到基础。

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.

撞了怀 2024-12-11 11:31:19

您提供的错误消息实际上表明相关线索的线索为空。请访问某处。您已经定义了“潜在客户”,但其“访问”尚未定义。

您确定可以像 :visit.exists 中那样使用 .exists 吗?您似乎收到了一些实际上没有该访问字段的潜在客户。

要进行检查,您可以尝试类似

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

“请检查这是否有效”之类的操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文