Reputation: 6862
I have three models User
, Supplier
, Company
. There is One to Many relationship between User
and Supplier
and, Company
and Supplier
(a user has many supplier but a supplier belongs to one user). What would be right way to create such association in Rails 3? Also a User has_many Courses through UserCourses
Upvotes: 0
Views: 48
Reputation: 11
If Courses is another model along with User,Company and Supplier, then following should work.
User: has_many :suppliers has_many :user_courses has_many :courses, :through => :user_courses
Supplier: belongs_to :user belongs_to :company
Company: has_many :suppliers
Courses: has_many :user_courses has_many :users, :through => :user_courses
Upvotes: 1
Reputation: 10738
If I got you correctly, it should be something like this:
User:
has_many :suppliers
has_many :user_courses
has_many :users, through: :user_courses
Supplier:
belongs_to :user
belongs_to :company
Company:
has_many :suppliers
Upvotes: 1