있습니다레일 협회 쿼리
create_table :schools do |t|
t.string :name
t.timestamps
end
create_table :variables do |t|
t.string :name
t.string :category
t.timestamps
end
create_table :data do |t|
t.string :entry
t.decimal :rank, :scale => 3
t.integer :school_id, :null => false
t.integer :variable_id, :null => false
t.timestamps
end
모델 클래스 : 여기
class Datum < ActiveRecord::Base
belongs_to :school
belongs_to :variable
end
class School < ActiveRecord::Base
has_many :data
has_many :variables, :through => :data
end
class Variable < ActiveRecord::Base
has_many :data
has_many :schools, :through => :data
end
내 학교 show.html.erb 페이지는 현재 :
<h2> <%= @school.name %> </h2>
<table>
<% @school.variables.each do |variable| %>
<tr><tr/>
<td><%= variable.name %></td>
<td><%= Datum.find(:first, :conditions => {:school_id => @school.id, :variable_id => variable.id}).entry %></td>
<td><%= link_to 'Edit', edit_school_path(@school) %></td>
<td><%= link_to 'Back', schools_path %></td>
<% end %>
</table>
그것은 내가 원하는 것이지만 많은 queri es. 나는 열심히 선적해야한다고 가정하고 있지만, 온라인에서 찾은 예제를 기반으로이 작업을 수행하는 방법을 알 수 없었습니다 (저는 레일이 처음이었습니다). 누구든지 아이디어가 있습니까?