开发者

Ruby on Rails: has_many referential --which model objects does it own?

开发者 https://www.devze.com 2023-03-31 12:25 出处:网络
I am new to Rails and finished Michael Hartl\'s \"Ruby on Rails 3 Tutorial\". Although the book teaches me a lot, I find this puzzle I don\'t understand.

I am new to Rails and finished Michael Hartl's "Ruby on Rails 3 Tutorial". Although the book teaches me a lot, I find this puzzle I don't understand.

To preview the puzzle, that is, I don't understand, inside User model,

has_many :following, :through=>:relationship, :source=>:followed

how this piece of code link "user.following" to an array of User instances.

And below is the whole puzzle.

First of all, I have the Relationship model, which records followed_id and follower_id infos. Inside Relationship model, the association is simple as

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"
end

Then, inside the User model, a user will assume the role of follower, and collect all its following rows in relationships table through relationships association.

class User < ActiveRecord::Base
  .
  .
  .
  has_many :relationships, :foreign_key =开发者_运维技巧> "follower_id", :dependent => :destroy
  .

Until now, I got it.

But confusion came at the next line, where through user.following it can assemble all that user's following(User instances). Like so,

has_many :following, :through=>:relationships, :source=>:followed

I understand that :source=>:followed will overwrite the default, and let find all followed_ids associated with that user.

But, how can Rails recognize followed_id to link to User object? The label name doesn't match users, nor is there :class_name specified. I just don't get how Rails do this underlying work, or I missed out some hints.

Thank you! :)


But, how can Rails recognize followed_id to link to User object? The label name doesn't match users, nor is there :class_name specified. I just don't get how Rails do this underlying work, or I missed out some hints.

Rails recognize that is an user object because it is set in Relationship's belongs_to. What Rails does here is to follow the relationship class through the foreign key "follower_id" and returning every User that has a relationship with the current user as followed. Of course Rails do that in a single SQL statement like this:

SELECT `users`.* FROM `users` INNER JOIN `relationships` ON `relationships`.followed_id = `users`.id WHERE ((`relationships`.follower_id = <the current user id>  ))


has_many :following, :through=>:relationships, :source=>:followed

This explains to Rails that following is the inverse relationship of following and that users has many following and followed through his relationships.

The way Rails knows that followed_id is linked to User is that it is defined in your Relationship model.

Hope you've understood ! Good luck :)

0

精彩评论

暂无评论...
验证码 换一张
取 消