I'm reading Service Oriented Design with Ruby on Rails, which includes the code you see in this image
My question is specifically about this line from the User model
has_many :followers, :through -> :followings, :source => :user
in which a User (as in an instance of the user model) has_many :followers, which are really just other users. So a user has_many users. This seems very weird to me that a User has_many :users, as if something should break when you write that, so I thought doign has_many :followers ...:source => :user might be meant to stop things from breaking.
According to one answer on this SO question Need help to understand :source option of has_one/has_many through of Rails, which was about the :source method for has_many associations, one rationale for using :source is to take code that looks this
class Newsletter
has_many :subscriptions
has_many :users, :through => :subscriptions
end
and change it into this
class Newsletter
has_many :subscriptions
has_many :subscribers, :through => :subscriptions, :source => :user
end
so that instead of doing this
Newsletter.find(id).users
one would do the much more logical (because Newsletters have subscribers not users)
Newsletter.find(id).subscribers
So, returning to the code from the book, my question is, is the only rationale for doing this in the User model
has_many :followers, :through -> :followings, :source => :user
to make more readable code User.find(id).followers
? There would otherwise be no problem doing
User.rb
has_many :users
Because if Rails let you do that (i.e. it doesn't break the application), then where would you put the belongs_to?
User.rb
has_many :users
belongs_to :user
This would seem very weird to me, but if :source is only there to let you write clearer queries, then this code should still work?
Update: the point of the question is, is there anything stopping anyone from doing this?
User.rb
has_many :users
belongs_to :user