【发布时间】:2011-06-12 10:43:52
【问题描述】:
我有 2 个模型,汽车和注册。
class Car < ActiveRecord::Base
belongs_to :Registration
end
class Registration < ActiveRecord::Base
has_many :cars, :dependent => :destroy
accepts_nested_attributes_for :cars, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
在 CarsController 中:
def index
@cars = Car.all
@cars2 = Car.all(:joins => :Registration)
end
在视图中:
<% @cars.each do |car| %>
<tr>
<td><%= car.twitter %></td>
<td><%= car.facebook %></td>
<td>
<% @cars2.Registration.each do |h| %> #here is my problem
<%= h.email %>
<% end %>
</td>
</tr>
<% end %>
这是我对汽车的陈述。我正在尝试打印每个车主的电子邮件。电子邮件在表 Registration(模型注册) 中。我不知道如何查询数据库,我想从表 Registrations 中获取电子邮件,当表 Cars 中的列 registration_id == 表 Registrations 的 id 列时...
所以我想问你,如果你有小费,怎么办...
【问题讨论】:
标签: mysql ruby-on-rails-3 join view