Brian O Halloran
Brian O Halloran

Reputation: 57

Relationship model between users, companies and introductions

Apologies for the newbie question but I am completely stumped on this one.

I have two models, user and company, and I am trying to establish a "request_introduction" relationship between them.

A user can have one intro_request with many companies while a company can have one intro_request from many users.

Thanks in advance.

Upvotes: 2

Views: 162

Answers (1)

iceveda06
iceveda06

Reputation: 619

Brian, It sounds like you want to do many-to-many relationship between 'user' and 'company'. You can set this by creating a :through association.

It should something similar to this:

class User < ApplicationRecord
  has_many :introductions
  has_many :customers, through: :introductions
end

class Customer < ApplicationRecord
  belongs_to :users
  belongs_to :customers
end

class Introduction < ApplicationRecord
  has_many :users
  has_many :customers, through: :users
end

Here is a link to the rails guide: (section 2.4) http://guides.rubyonrails.org/association_basics.html

Upvotes: 2

Related Questions