Bhushan Lodha
Bhushan Lodha

Reputation: 6862

Association in Rails 3

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

Answers (2)

user1554542
user1554542

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

davidrac
davidrac

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

Related Questions