我正在使用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 %>
我该怎么解决这个问题?
我认为问题是,当你这样做时,你正在将孩子和妈妈合并:
.merge(Mom.near(@user_location, 100))
因此,除了Kid
对象,@kids
还将包含Mom
对象,而这些Mom
对象没有您期望的属性。可以使用@kids.inspect
进行调试。