Aliasing with many_to_many relation in Rails 4 -


i have 2 classes: user , folder use has_many :through create many_to_many relationship. need have aliases both classes such subscriber alias user , subscription alias folder. in end, able say:

user.subscriptions  

and

folder.subscribers 

is doable? have defined join class subscriptionsubscriber like

class subscriptionsubscriber < activerecord::base   belongs_to :subscription , class_name: "folder"   belongs_to :subscriber , class_name: "user" end 

and added following code user , folder

class user < activerecord::base   has_many :subscription_subscribers   has_many :subscriptions, :through => :subscription_subscribers, :class_name => "folder", :foreign_key => "subscription_id", :source => :folder end  class folder < activerecord::base   has_many :subscription_subscribers   has_many :subscribers, :through => :subscription_subscribers, :class_name => "user", :foreign_key => "subscriber_id", :source => :user end 

this not working. example, if run

user.subscription

i following error

activerecord::hasmanythroughsourceassociationnotfounderror:  not find source association(s) :folder in model subscriptionsubscriber.  try 'has_many :subscriptions, :through => :subscription_subscribers, :source => <name>'.  1 of subscription or subscriber? 

any highly appreciated. thanks.

with :source, we're telling rails association called :subscription on subscriptionsubscriber model.

class user < activerecord::base   has_many :subscription_subscribers, foreign_key: "subscriber_id"   has_many :subscriptions, through: :subscription_subscribers, source: :subscription end  class folder < activerecord::base   has_many :subscription_subscribers, foreign_key: 'subscription_id'   has_many :subscribers, through: :subscription_subscribers, source: :subscriber end 

after making change, try fetching user.subscriptions


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -