Hi, this is my first post so please bear with me.
When I started learning rails, I struggled a lot of times understanding & learning associations. But I didn't stop trying because it's so freaking awesome and so full of magic (well, that's what I thought). So I am here to demonstrate a complex association wherein we have a model called User and another model called Relationship to simulate a follow feature similar to Twitter.
I know a lot of you guys already knew this but I'd like to share my own understanding of Rails' associations. For beginners out there, this might help!
class User < ActiveRecord::Base #let's just make it simple and only add one column for this model called 'username' attr_accessible :username #use "follower_id" as the foreign key since by default it will use "user_id" which does #not exist inside the "relationships" table has_many :relationships, foreign_key: :follower_id #find all user with the ID matching the "followed_user_ids" of the result. If I did not #include "source: :followed_user", it will look for the column "following_id" which is wrong has_many :followings, through: :relationships, source: :followed_user #here we are just reversing the relationship. Instead of using "follower_id", we now use #"followed_user_id" has_many :inverse_relationships, class_name:"Relationship", foreign_key: :followed_user_id #this line will look for the "follower_id" based on the result of the "inverse_relationships". #no "source" option is required since we have used the "followers" in the association thus #it looks for a "follower_id" on the "relationships" table which it does has_many :followers, through: :inverse_relationships end
class Relationship < ActiveRecord::Base #Here, class_name just means that it's a User model. belongs_to :follower, class_name: "User" belongs_to :followed_user, class_name: "User" end
Hope this helps!