hellorb
hellorb

Reputation: 3

Ruby: ActiveRecord relationship

I have some problems setting up my desired relationship in my application. Some help and hints would be appreciated!

I have the following models:

The relationship is supposed to connect the user to many companies. Company has_many campaigns.

I want to to connect all the campaigns related to the companies that the specific user follows.

Users > (Relationships) > Companies > Campaigns

I'd better not post some code since it's just a mess and not at all doing what I want.

I've also really tried to follow railstutorial.org, http://ruby.railstutorial.org/chapters/following-users#top and change it the way I want with no success.

I need your help. :)

Upvotes: 0

Views: 122

Answers (1)

jbarket
jbarket

Reputation: 882

Should be pretty straightforward! This is obviously pseudocode, but here you go:

User
    has_many :relationships
    has_many :companies, :through => :relationships
    has_many :campaigns, :through => :companies

Relationship
    belongs_to :user
    belongs_to :company

Company
    has_many :relationships
    has_many :users, :through => :relationships
    has_many :campaigns

Campaign
    belongs_to :company

Upvotes: 1

Related Questions