【发布时间】:2014-04-12 08:18:11
【问题描述】:
我正在使用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 %>
我该如何解决这个问题?
【问题讨论】:
-
发布您的
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