I am having trouble accessing the correct information in my rails model (which I believe to be correct) The schema for my tables are
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
Model classes:
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
here is my school show.html.erb page currently:
<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>
It does what I want it to but it does wayy to many queries. I'm assuming I have to do eager loading but based on examples I found online I couldn't figure out how to do this (I'm really new to rails). Does anyone have any ideas?