Skip to content

Instantly share code, notes, and snippets.

@ekampf
Created March 8, 2012 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekampf/2000771 to your computer and use it in GitHub Desktop.
Save ekampf/2000771 to your computer and use it in GitHub Desktop.
Basically, the has_many association controls how the "join model" (Subscription) is created and the has_many_through association controls how the :through model is created. So instead of having one has_many :subscriptions I split it to two...
has_many :subscriptions_members,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::MEMBER},
:dependent => :destroy
has_many :subscriptions_followers,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::FOLLOWER},
:dependent => :destroy
has_many :members, :through => :subscriptions_members,
:source => :user, :uniq => true
has_many :followers,:through => :subscriptions_followers,
:source => :user, :uniq => true
@arikfr
Copy link

arikfr commented Mar 8, 2012

And this sets the :kind field by itself?

@ekampf
Copy link
Author

ekampf commented Mar 8, 2012

Yes. the :conditions also control the object creation.
So subscriptions_members controls how the Subscription instance is created and members how a user will be created

@arikfr
Copy link

arikfr commented Mar 8, 2012

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment