简体   繁体   English

这甚至应该是has_many:through关联吗?

[英]Should this even be a has_many :through association?

A Post belongs_to a User, and a User has_many Posts. 一个帖子属于一个用户,而一个用户拥有多个帖子。
A Post also belongs_to a Topic, and a Topic has_many Posts. 帖子还属于一个主题,而主题也有很多帖子。

class User < ActiveRecord::Base
  has_many :posts
end

class Topic < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
   belongs_to :user
   belongs_to :topic
end

Well, that's pretty simple and very easy to set up, but when I display a Topic, I not only want all of the Posts for that Topic, but also the user_name and the user_photo of the User that made that Post. 好吧,这很简单,也很容易设置,但是当我显示一个主题时,我不仅想要该主题的所有帖子,还想要发布该帖子的用户的user_name和user_photo。 However, those attributes are stored in the User model and not tied to the Topic. 但是,这些属性存储在用户模型中,并且与主题无关。 So how would I go about setting that up? 那么我将如何进行设置?

Maybe it can already be called since the Post model has two foreign keys, one for the User and one for the Topic? 由于Post模型具有两个外键,一个用于用户,一个用于主题,也许已经可以调用它了?

Or, maybe this is some sort of "one-way" has_many through assiociation. 或者,也许这是某种通过联系的“单向” has_many。 Like the Post would be the join model, and a Topic would has_many :users, :through => :posts. 就像Post是联接模型一样,Topic将具有has_many:users,:through =>:posts。 But the reverse of this is not true. 但这并非如此。 Like a User does NOT has_many :topics. 像用户一样,没有has_many:topics。

So would this even need to be has_many :though association? 因此,甚至需要has_many:though关联吗? I guess I'm just a little confused on what the controller would look like to call both the Post and the User of that Post for a give Topic. 我想我对控制器为给定主题同时调用该帖子和该帖子的用户的外观感到有些困惑。

Edit: Seriously, thank you to all that weighed in. I chose tal's answer because I used his code for my controller; 编辑:真的,非常感谢您的参与。我选择了tal的答案,因为我将他的代码用作控制器。 however, I could have just as easily chosen either j.'s or tim's instead. 但是,我可以轻松选择j。或tim。 Thank you both as well. 也谢谢你们 This was so damn simple to implement. 这太容易实现了。 I think today might mark the beginning of my love affair with rails. 我认为今天可能标志着我与铁轨恋爱的开始。

you can get the user_name and user_photo when displaying a topic and its posts... 您可以在显示主题及其帖子时获取user_nameuser_photo ...

something like: 就像是:

@topic.posts.each do |post|
   user_name = post.user.name
   user_photo = post.user.photo
end

it's simple. 这很简单。 sorry if i didn't understand your question very well. 抱歉,如果我不太了解您的问题。

Well, if what you want is display the user name of the author of a post, for example, you can do something like (not tested, but should work) : 好吧,例如,如果您想要显示帖子作者的用户名,则可以执行以下操作(未经测试,但应该可以):

@posts = topic.posts.all(:include => :user) 

it should generate one request for all posts, and one for users, and the posts collection should have users. 它应该为所有帖子生成一个请求,为用户生成一个请求,并且帖子集合中应该有用户。

in a view (haml here) : 在一个视图中(在这里哈姆尔):

- @posts.each do |post|
  = post.user.name
  = post.body

If I understand your question correctly, no, a has_many :through association is not required here. 如果我正确理解了您的问题,则不需要,这里不需要has_many:through关联。

If you wanted to display a list of all users that posted in a topic (skipping the post information), then it would be helpful. 如果您想显示主题中发布的所有用户的列表(跳过发布信息),则将很有帮助。

You'll want to eager load the users from posts, though, to prevent n+1 queries. 但是,您将希望从帖子中加载用户,以防止出现n + 1个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM