Creating multiple one to many relationships in rails?

It appears that you're attempting to have two tables that inherit the location class, browsing_location and home_location and two tables that inherit the user class, browsing_user and home_user. For Rails 3: You've got the general idea, but it appears that you have mixed things up a bit. :source is used for many to many relationships to determine which association to use.

What you appear to need instead is :class_name I would need to see your table definitions for users and locations to make sure you're using :foreign_key attribute correctly user. Rb class User :creator_id # I'm assuming the locations belong to the user, as you're end goal was stated as # the ability to call set user. Browsing_location and user.

Home_location # that would mean that browsing_location and home_location would need to belong to # the User class, not the other way around. Has_many :browsing_locations, :class_name => :location has_many :home_locations, :class_name => :location end class Location :users # Users who are living here now has_many :home_users, :class_name => :users end.

It appears that you're attempting to have two tables that inherit the location class, browsing_location and home_location and two tables that inherit the user class, browsing_user and home_user. For Rails 3: You've got the general idea, but it appears that you have mixed things up a bit. :source is used for many to many relationships to determine which association to use.

What you appear to need instead is :class_name I would need to see your table definitions for users and locations to make sure you're using :foreign_key attribute correctly. User. Rb class User :creator_id # I'm assuming the locations belong to the user, as you're end goal was stated as # the ability to call set user.

Browsing_location and user. Home_location # that would mean that browsing_location and home_location would need to belong to # the User class, not the other way around. Has_many :browsing_locations, :class_name => :location has_many :home_locations, :class_name => :location end class Location :users # Users who are living here now has_many :home_users, :class_name => :users end.

Thanks - I think this is what I need. Trying it out now. Is active record doing magic behind the scenes to create the extra tables (because we have has_many with the reverse relationship a has_many as well).

Or is that what you mean by needing to see my table definitions, to make sure I have it all set up properly? – Mathieu Tozer Jan 29 at 0:59 The has_many relationships you've created aren't technically reverses of each other, as the classes belonging to Location are of type User, but not technically the User class. Otherwise you'd have a many to many relationship, and you'd need to do either a has_many :through or a has_and_belongs_to_many relationship.

– Paul Pettengill Jan 29 at 6:18 I wanted to see your table definitions to verify your foreign_key definition more than anything. You typically don't need to define the foreign_key, but it appears you're doing it correctly. – Paul Pettengill Jan 29 at 6:18 In terms of what ActiveRecord is doing with the :class_name is creating new joins to the corresponding table associated with that class.

– Paul Pettengill Jan 29 at 6:23.

I've done a lot of googling around and all the information is contradictory, or explains setting up many to many relationships, and explains methods using an intermediary table. But really all the database should need is for the user table to have two differently names id fields for the locations. Will something like the following work?

Quite a lot of my models will need relationships like this so I would like to avoid having to create extra tables for this.

I cannot locate a 13 story haunted house in Arizona. The only 13 store one I can find is in Georgia.

13th Floor Haunted House unless noted otherwise. All rights reserved. Site design by SinisterVisions.com.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions