Ruby: 1.9.2
Rails: 3.0beta3
I need some help with associations in Rails 3.
I have the following models (see excerpts below):
School, State, SchoolLocale
The schools table has the following fields:
id, name, state_id, school_locale_id
The states table has the following fields:
id, abbr, name
The school_locales table has the following fields:
id, code, name
Unfortunately, my data-source didn't have IDs for school_locales. Thus, the data stored in the 'school_locale_id' field in the schools table actually maps to the 'code' field in the school_locales table.
school.rb:
class School < ActiveRecord::Base
belongs_to :state
belongs_to :school_locale
end
state.rb:
class State < ActiveRecord::Base
has_many :schools
end
school_locale.rb:
class SchoolLocale < ActiveRecord::Base
has_many :schools
end
I would like a query for a given school, let's say School.find(1), that would output the school name, the state name and the school-locale name. I assume that I need to add an index to the 'code' field in the school_locales table and somehow specify it as a foreign key, but I'm not certain. Any help would be appreciated.