이 오류를 받고 있어요 :정의되지 않은 메서드 []에 대한`variety_id '액티브 :: 관계
undefined method `variety_id' for []:ActiveRecord::Relation
난 내 재판을 <% = r.results.variety_id %> 코드를 추가 해요 전망. < % = r.site.site_name %>은 정상적으로 작동하지만 < % = r.results.variety_id %>가 작동하지 않는 이유는 없습니다. 결과 데이터베이스에는 Trials 데이터베이스와 일치시키고 평가판보기에 나열하려는 많은 trial_id (3-6)가 있습니다.
시험보기
<% @results.each do |y| %>
<h1><%= y.year %> Trials</h1>
<% end %>
<hr>
<% @results.each do |s| %>
<h3><%= s.site.site_name %></h3>
<% end %>
<hr>
<% @results.each do |r| %>
<%= r.results.variety_id %>
<% end %>
컨트롤러
def trial
@results = Trial.where(params[:trial_id])
end
모델
class Trial < ActiveRecord::Base
attr_accessible :trial_id, :site_id, :year
scope :year, ->(year) { where(year: year) }
scope :trial_id, ->(trial_id) { where(trial_id: trial_id) }
belongs_to :site, :primary_key => 'site_id'
has_many :results
end
class Result < ActiveRecord::Base
attr_accessible :trial_id, :variety_id, :year
belongs_to :trial
end
lass Site < ActiveRecord::Base
attr_accessible :site_id, :site_name, :region
has_many :trials
end
스키마
create_table "results", :force => true do |t|
t.integer "trial_id"
t.integer "variety_id"
t.string "year"
end
create_table "sites", :force => true do |t|
t.integer "site_id"
t.string "site_name"
t.integer "region"
end
create_table "trials", :force => true do |t|
t.integer "trial_id"
t.integer "site_id"
t.string "year"
end
결과 및 평가판의 trial_id 열에 해당 ID가 미리 채워져 있습니다.
응원 jvperrin한다. 대접, 많은 감사를 일했습니다. – Grinskull