【问题标题】:ActiveModel::MissingAttributeError when joining and merging parent model? [duplicate]加入和合并父模型时出现 ActiveModel::MissingAttributeError? [复制]
【发布时间】:2014-04-12 08:18:11
【问题描述】:

我正在使用GeocoderWill Paginate。我正在尝试比较用户地址和孩子妈妈地址之间的距离。这是我的模型:

class User
 has_many :kids
 geocoded_by :address
end

class Kid
  belongs_to :mom
  belongs_to :dad
end

class Mom
  has_many :kids
  has_many :dads, through: :kids
  geocoded_by :address
end

class Dad
  has_many :kids
  has_many :moms, through: :kids
end

比我如何尝试让它发挥作用:

class ApplicationController
  before_filter :set_user_location

  private

  def set_user_location
    if signed_in?
      @user_location = current_user.address
    end
  end
end

class DadsController

def show
  @dad = Dad.find(params[:id])
  @kids = @dad.kids.joins(:mom).merge(Mom.near(@user_location, 100)).order(name: :asc, created_at: :desc).paginate(page: params[:page], per_page: 25)
end

现在有了所有这些。我遇到了错误:

ActiveModel::MissingAttributeError - missing attribute: birthday:
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:95:in `block (2 levels) in read_attribute'
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:94:in `fetch'
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:94:in `block in read_attribute'
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `fetch'
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `read_attribute'
  activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:59:in `__temp__57e69647'
  app/views/dads/show.html.erb:27:in `block in _app_views_dads_show_html_erb__397094631_92677670'
  activerecord (4.0.2) lib/active_record/relation/delegation.rb:13:in `each'
  activerecord (4.0.2) lib/active_record/relation/delegation.rb:13:in `each'
............

如果我从循环中删除 kid.birthday,它会说下一个属性是问题,直到我的 dads/show.html.erb 上没有任何属性。

<% @kids.each do |kid| %>
   <%= kid.birthday %>
   <%= kid.name %>
   <% if kid.upkeep.present? %>
     <%= kid.upkeep %>
     <%= kid.upkeep_size %> 
   <% end %>      
   <%= kid.age %>
   <%= kid.favorite_color %>
<% end %>

我该如何解决这个问题?

【问题讨论】:

  • 发布您的show.html.erb
  • @Pavan 好的,完成了。
  • DB-struct 如何查找“kid”表?您是否可能忘记运行 rake db:migrate?
  • @gernberg 如果我将方法转换为@kids = @dad.kids.order(name: :asc, created_at: :desc).paginate(page: params[:page], per_page: 25),它就可以工作。我的数据库 Kid 表有假数据,所以迁移是正确的。
  • 那个查询不包括“生日”——你确定你的数据库中有那个字段吗?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:

我认为问题在于,当你这样做时,你会把孩子和妈妈结合起来:

.merge(Mom.near(@user_location, 100))

因此,除了 Kid 对象之外,@kids 还将包含 Mom 对象,而那些 Mom 对象不具有您期望的属性。您可以使用@kids.inspect 进行调试。

【讨论】:

  • 是的,我也有同样的想法,但现在我不确定如何纠正这个问题。
  • @user2784630 添加了如何解决此问题的建议。
  • 由于应用程序设计,我不能从妈妈开始。它必须通过父亲。我尝试了上述方法,但它返回一个 ActiveRecord 关系错误,kids 未定义。我也尝试在我的其他范围内使用includes(:kids),但它会导致一切都去NULL
  • 我更新的问题是Mom.near返回一个集合,所以你不能只调用.kids就可以了。我会考虑另一种解决方案。
猜你喜欢
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多