这是否应该是一个 has_many :through 关联?
一个帖子属于一个用户,一个用户有很多帖子。
一个帖子也属于一个主题,一个主题有很多帖子。
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
嗯,这非常简单并且很容易设置,但是当我显示一个主题时,我不仅想要该主题的所有帖子,还想要创建该帖子的用户的 user_name 和 user_photo 。但是,这些属性存储在用户模型中,并且与主题无关。那么我该如何设置呢?
也许它已经可以被调用了,因为 Post 模型有两个外键,一个用于用户,一个用于主题?
或者,也许这是某种通过关联的“单向”has_many。就像帖子是连接模型一样,主题是 has_many :users, :through => :帖子。但反过来则不然。就像用户没有 has_many :topics 一样。
那么这是否需要 has_many :though 关联?我想我只是对控制器调用给定主题的帖子和该帖子的用户的样子有点困惑。
编辑:说真的,谢谢大家的参与。我选择了 tal 的答案,因为我使用了他的代码作为我的控制器;然而,我也可以轻松地选择 j.'s 或 tim's。也谢谢你们俩。这实施起来非常简单。我想今天可能标志着我对 Rails 的热爱的开始。
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. 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?
Or, maybe this is some sort of "one-way" has_many through assiociation. Like the Post would be the join model, and a Topic would has_many :users, :through => :posts. But the reverse of this is not true. Like a User does NOT has_many :topics.
So would this even need to be has_many :though association? 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; however, I could have just as easily chosen either j.'s or tim's instead. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当显示主题及其帖子时,您可以获取
user_name
和user_photo
...类似:
这很简单。抱歉,如果我不太明白你的问题。
you can get the
user_name
anduser_photo
when displaying a topic and its posts...something like:
it's simple. sorry if i didn't understand your question very well.
好吧,例如,如果您想要显示帖子作者的用户名,您可以执行类似的操作(未经测试,但应该可行):
它应该为所有帖子生成一个请求,为用户生成一个请求,并且帖子集合应该有用户。
在视图中(此处为 haml):
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) :
it should generate one request for all posts, and one for users, and the posts collection should have users.
in a view (haml here) :
如果我正确理解你的问题,不,这里不需要 has_many :through 关联。
如果您想显示在某个主题中发帖的所有用户的列表(跳过发帖信息),那么它会很有帮助。
不过,您需要立即从帖子中加载用户,以防止 n+1 查询。
If I understand your question correctly, no, a has_many :through association is not required here.
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.