我正在尝试从我的 rails 应用程序中的另一个表中引用一列。我想使用两个表(站点和试验)中匹配的 site_id 列来引用站点名称。
我的控制器
def list
@list = Trial.year(params[:year]).order(:region_id)
end
我的模特
class Trial < ActiveRecord::Base
attr_accessible :trial_id, :site_id, :year, :trial_type, :region_id
scope :year, ->(year) { where(year: year) }
scope :trial_id, ->(trial_id) { where(trial_id: trial_id) }
belongs_to :site
end
class Site < ActiveRecord::Base
attr_accessible :site_id, :site_name, :region
has_many :trials
end
我的观点
<table class="table">
<th>Trial Location</th>
<th>Trial Type</th>
<th>Grower</th>
<% @list.each do |list| %>
<tr>
<td>
<%= link_to list.site.site_name, trial_trials_path(trial_id: list.trial_id) %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
<td>
<%= link_to list.trial_type, trial_trials_path(trial_id: list.trial_id) unless list.trial_type.blank? %>
</td>
</tr>
<% end %>
</table>