Suppose I have one table: states id statename And I have two other tables that contains states: houses id color state_id places id place_name state_id How would my model relationships look like? class State < ActiveRecord::Base belongs_to house belongs_to place end class House < ActiveRecord::Base has_one state end class Place < ActiveRecord::Base has_one state end Is that right? Can I have a model that belongs to multiple models? -- Vincent H Gov -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/b1163cba/attachment.html
On 3/22/06, Vincent Gov <vincehgov@gmail.com> wrote:> Suppose I have one table: > > states > id > statename > > And I have two other tables that contains states: > > houses > id > color > state_id > > places > id > place_name > state_id > > How would my model relationships look like? > > class State < ActiveRecord::Base > belongs_to house > belongs_to place > end > > class House < ActiveRecord::Base > has_one state > end > > class Place < ActiveRecord::Base > has_one state > end > > Is that right? Can I have a model that belongs to multiple models? >You can have as many belongs_to associations as you want in one model, but you don''t want that with the table structure you sketched out. The ActiveRecord setup you want is probably: class State < ActiveRecord::Base has_one :place has_one :house end class Place < ActiveRecord::Base belongs_to :state end class House < ActiveRecord::Base belongs_to :state end Put "belongs_to :name" in the class that wraps a table containing a "name_id" column. --Wilson.
That should probably be class State <ActiveRecord::Base has_many :places has_many :houses end Unless you only want one house and one place per state... On 3/23/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:> > On 3/22/06, Vincent Gov <vincehgov@gmail.com> wrote: > > Suppose I have one table: > > > > states > > id > > statename > > > > And I have two other tables that contains states: > > > > houses > > id > > color > > state_id > > > > places > > id > > place_name > > state_id > > > > How would my model relationships look like? > > > > class State < ActiveRecord::Base > > belongs_to house > > belongs_to place > > end > > > > class House < ActiveRecord::Base > > has_one state > > end > > > > class Place < ActiveRecord::Base > > has_one state > > end > > > > Is that right? Can I have a model that belongs to multiple models? > > > > You can have as many belongs_to associations as you want in one model, > but you don''t want that with the table structure you sketched out. > > The ActiveRecord setup you want is probably: > class State < ActiveRecord::Base > has_one :place > has_one :house > end > > class Place < ActiveRecord::Base > belongs_to :state > end > > class House < ActiveRecord::Base > belongs_to :state > end > > Put "belongs_to :name" in the class that wraps a table containing a > "name_id" column. > > --Wilson. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/95acefa5/attachment.html
Sorry, bad example. I just wanted to know if i can have multiple belongs_to statements. Thanks guys On 3/22/06, Liquid <has.sox@gmail.com> wrote:> > That should probably be > class State <ActiveRecord::Base > has_many :places > has_many :houses > end > > Unless you only want one house and one place per state... > > > > > On 3/23/06, Wilson Bilkovich <wilsonb@gmail.com> wrote: > > > > On 3/22/06, Vincent Gov <vincehgov@gmail.com> wrote: > > > Suppose I have one table: > > > > > > states > > > id > > > statename > > > > > > And I have two other tables that contains states: > > > > > > houses > > > id > > > color > > > state_id > > > > > > places > > > id > > > place_name > > > state_id > > > > > > How would my model relationships look like? > > > > > > class State < ActiveRecord::Base > > > belongs_to house > > > belongs_to place > > > end > > > > > > class House < ActiveRecord::Base > > > has_one state > > > end > > > > > > class Place < ActiveRecord::Base > > > has_one state > > > end > > > > > > Is that right? Can I have a model that belongs to multiple models? > > > > > > > You can have as many belongs_to associations as you want in one model, > > but you don''t want that with the table structure you sketched out. > > > > The ActiveRecord setup you want is probably: > > class State < ActiveRecord::Base > > has_one :place > > has_one :house > > end > > > > class Place < ActiveRecord::Base > > belongs_to :state > > end > > > > class House < ActiveRecord::Base > > belongs_to :state > > end > > > > Put "belongs_to :name" in the class that wraps a table containing a > > "name_id" column. > > > > --Wilson. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Vincent H Gov -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/0703e66d/attachment-0001.html
Is it correct to say that the has_one relations are not necessary unless you are going to be reading the state table and want to get all the houses that are in that state? I.e. state.houses On Wednesday, March 22, 2006, at 10:06 PM, Wilson Bilkovich wrote:>On 3/22/06, Vincent Gov <vincehgov@gmail.com> wrote: >> Suppose I have one table: >> >> states >> id >> statename >> >> And I have two other tables that contains states: >> >> houses >> id >> color >> state_id >> >> places >> id >> place_name >> state_id >> >> How would my model relationships look like? >> >> class State < ActiveRecord::Base >> belongs_to house >> belongs_to place >> end >> >> class House < ActiveRecord::Base >> has_one state >> end >> >> class Place < ActiveRecord::Base >> has_one state >> end >> >> Is that right? Can I have a model that belongs to multiple models? >> > >You can have as many belongs_to associations as you want in one model, >but you don''t want that with the table structure you sketched out. > >The ActiveRecord setup you want is probably: >class State < ActiveRecord::Base > has_one :place > has_one :house >end > >class Place < ActiveRecord::Base > belongs_to :state >end > >class House < ActiveRecord::Base > belongs_to :state >end > >Put "belongs_to :name" in the class that wraps a table containing a >"name_id" column. > >--Wilson. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-- Posted with http://DevLists.com. Sign up and save your time!