RoR associations through or not through?
Posted
by showFocus
on Stack Overflow
See other posts from Stack Overflow
or by showFocus
Published on 2010-04-08T02:43:26Z
Indexed on
2010/04/08
2:53 UTC
Read the original article
Hit count: 399
I have four models that are related to one another, the way I have it setup at the moment is I have to select a county, region and country when entering a new city.
class Country < ActiveRecord::Base
has_many :regions
has_many :counties
has_many :cities
end
class Region < ActiveRecord::Base
has_one :country
has_many :counties
has_many :cities
end
class County < ActiveRecord::Base
has_one :country
has_one :region
has_many :cities
end
class City < ActiveRecord::Base
has_one :country
has_one :region
has_one :county
end
Would it be better to use the :through
symbol in the association? So I could say the city:
has_one :country, :through => :region
Not sure if this is correct, I have read how :through works but I'm not sure if this is the best solution.
I am a newbie and while I'm not struggling with the syntax and how things work, it would be good to get opinions on best practices and the way things should be done from some rails wizards!
Thanks in advance.
© Stack Overflow or respective owner