두 모델 자동차 및 등록이 있습니다. CarsController에서2 개의 테이블 중 조인이있는 레일 3
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
:
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_id 자동차 테이블 등록의 id 열 ...
그래서 나는하고 싶다. 당신이 열망로드를 원하는 경우
<%= car.registration.email %>
는 다음 컨트롤러에 다음 줄을 대신 사용 : 이메일을 가져 오기 위해 ... 당신이 팁이있는 경우, 어떻게 할 방법이 대신
젠장, 어리석은 잘못 .. . – user1946705