我正在使用Geocoder和Will 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 %>
我该如何解决这个问题?
答案 0 :(得分:1)
我认为问题在于,当你这么做时,你会把孩子和妈妈合并:
.merge(Mom.near(@user_location, 100))
因此,除Kid
个对象外,@kids
还会包含Mom
个对象,而这些Mom
个对象不具备您期望的属性。您可以使用@kids.inspect
进行调试。