210

Please help me in understanding the :source option of has_one/has_many :through association. The Rails API explanation makes very little sense to me.

"Specifies the source association name used by has_many :through => :queries. Only use it if the name cannot be inferred from the association. has_many :subscribers, :through => :subscriptions will look for either :subscribers or :subscriber on Subscription, unless a :source is given. "

3 Answers 3

282

Sometimes, you want to use different names for different associations. If the name you want to use for an association on the model isn't the same as the assocation on the :through model, you can use :source to specify it.

I don't think the above paragraph is much clearer than the one in the docs, so here's an example. Let's assume we have three models, Pet, Dog and Dog::Breed.

class Pet < ActiveRecord::Base
  has_many :dogs
end

class Dog < ActiveRecord::Base
  belongs_to :pet
  has_many :breeds
end

class Dog::Breed < ActiveRecord::Base
  belongs_to :dog
end

In this case, we've chosen to namespace the Dog::Breed, because we want to access Dog.find(123).breeds as a nice and convenient association.

Now, if we now want to create a has_many :dog_breeds, :through => :dogs association on Pet, we suddenly have a problem. Rails won't be able to find a :dog_breeds association on Dog, so Rails can't possibly know which Dog association you want to use. Enter :source:

class Pet < ActiveRecord::Base
  has_many :dogs
  has_many :dog_breeds, :through => :dogs, :source => :breeds
end

With :source, we're telling Rails to look for an association called :breeds on the Dog model (as that's the model used for :dogs), and use that.

4
  • 2
    I think you meant for your last class Animal to be called class Pet, just a typo I believe.
    – Kamilski81
    Feb 29, 2012 at 14:26
  • 3
    In the example above, should the the association under Dog be has_many :breed instead of :breeds and then the :source be :breed singular, to represent the model name, instead of :breeds which represents the table name? E.g. has_many :dog_breeds, :through => :dogs, :source => :breed (no s suffixing :breed)? Dec 30, 2014 at 22:22
  • 2
    I've tested this. it is singular, no s suffix in the :source =>
    – Anwar
    Oct 18, 2015 at 14:02
  • "In this case, we've chosen to namespace the Dog::Breed, because we want to access Dog.find(123).breeds as a nice and convenient association.". You don't need a namespace for that do you?
    – Jwan622
    Nov 10, 2017 at 16:36
229

Let me expand on that example:

class User
  has_many :subscriptions
  has_many :newsletters, :through => :subscriptions
end

class Newsletter
  has_many :subscriptions
  has_many :users, :through => :subscriptions
end

class Subscription
  belongs_to :newsletter
  belongs_to :user
end

With this code, you can do something like Newsletter.find(id).users to get a list of the newsletter's subscribers. But if you want to be clearer and be able to type Newsletter.find(id).subscribers instead, you must change the Newsletter class to this:

class Newsletter
  has_many :subscriptions
  has_many :subscribers, :through => :subscriptions, :source => :user
end

You are renaming the users association to subscribers. If you don't provide the :source, Rails will look for an association called subscriber in the Subscription class. You have to tell it to use the user association in the Subscription class to make the list of subscribers.

2
  • 4
    note that, the singularize model names should be used in :source =>, not plural. So, :users is wrong, :user is correct
    – Anwar
    Oct 18, 2015 at 14:04
  • 1
    This is the best answer!, let me just emphasize this part: "You are renaming the users association to subscribers. If you don't provide the :source, Rails will look for an association called subscriber in the Subscription class." Oct 30, 2017 at 0:53
19

Most simple answer:

Is the name of the relationship in the table in the middle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.