Ruby on Rails:has_many 引用——它拥有哪些模型对象?

发布于 2024-12-01 23:07:36 字数 1367 浏览 0 评论 0原文

我是 Rails 新手,已完成 Michael Hartl 的“Ruby on Rails 3 教程”。虽然这本书教了我很多,但我发现这个谜题我不明白。

要预览这个难题,也就是说,我不明白,在 User 模型内部,

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

这段代码如何将“user.following”链接到 User 实例数组。

下面是整个谜题。

首先,我有 Relationship 模型,它记录 followed_idfollower_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-12-08 23:07:36

但是,Rails 如何识别 follow_id 来链接到 User 对象呢?这
标签名称与用户不匹配,也没有指定 :class_name。我
只是不明白 Rails 如何完成这项基础工作,或者我错过了一些
提示。

Rails 识别出这是一个用户对象,因为它是在Relationship 的belongs_to 中设置的。 Rails 在这里所做的是通过外键“follower_id”跟踪关系类,并返回与当前用户有关系的每个用户,如下所示。当然,Rails 在单个 SQL 语句中执行此操作,如下所示:

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

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>  ))
没有伤那来痛 2024-12-08 23:07:36
has_many :following, :through=>:relationships, :source=>:followed

这向 Rails 解释说,followingfollowing 的逆关系,并且用户通过他的关系有很多关注和关注。

Rails 知道 followed_id 链接到 User 的方式是它是在您的 Relationship 模型中定义的。

希望你已经理解了!祝你好运 :)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文