Ruby on Rails:has_many 引用——它拥有哪些模型对象?
我是 Rails 新手,已完成 Michael Hartl 的“Ruby on Rails 3 教程”。虽然这本书教了我很多,但我发现这个谜题我不明白。
要预览这个难题,也就是说,我不明白,在 User 模型内部,
has_many :following, :through=>:relationship, :source=>:followed
这段代码如何将“user.following”链接到 User 实例数组。
下面是整个谜题。
首先,我有 Relationship 模型,它记录 followed_id 和 follower_id 信息。在Relationship模型中,关联很简单
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, :class_name => "User"
belongs_to :followed, :class_name => "User"
end
,如下> 关系表中的行通过关系关联。
class User < ActiveRecord::Base
.
.
.
has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy
.
直到现在,我明白了。
但下一行出现了混乱,通过 user.following 它可以组装该用户的所有关注者(用户实例)。像这样,
has_many :following, :through=>:relationships, :source=>:followed
我知道 :source=>:followed 将覆盖默认值,并让查找与该用户关联的所有 followed_ids 。
但是,Rails 如何识别 followed_id 链接到 User 对象?标签名称与 users 不匹配,也未指定 :class_name。我只是不明白 Rails 如何完成这项基础工作,或者我错过了一些提示。
谢谢你! :)
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! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 识别出这是一个用户对象,因为它是在Relationship 的belongs_to 中设置的。 Rails 在这里所做的是通过外键“follower_id”跟踪关系类,并返回与当前用户有关系的每个用户,如下所示。当然,Rails 在单个 SQL 语句中执行此操作,如下所示:
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:
这向 Rails 解释说,
following
是following
的逆关系,并且用户通过他的关系有很多关注和关注。Rails 知道
followed_id
链接到 User 的方式是它是在您的Relationship
模型中定义的。希望你已经理解了!祝你好运 :)
This explains to Rails that
following
is the inverse relationship offollowing
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 yourRelationship
model.Hope you've understood ! Good luck :)