为什么 User 模型不需要 has_many?
在“Rails 3 in Action”第 6 章第 6.5.1 节中,我们将票证与用户关联通过使用:
@ticket = @project.tickets.build(params[:id].merge!(:user => current_user))
并且 Ticket
模型具有此指令:belongs_to :user
,然后一切正常。
但是,为什么 User
模型不需要 has_many :tickets
来构建用户和主题之间的“一对多”关联?
In "Rails 3 in Action" Chapter 6, Section 6.5.1, we associated a ticket to a user by using:
@ticket = @project.tickets.build(params[:id].merge!(:user => current_user))
and the Ticket
model has this directive: belongs_to :user
, then it all works.
But, why does the User
model not need the has_many :tickets
to build a "one-to-many" association between user and topic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确定用户模型中没有
has_many
@user.topics
肯定不会工作,除非你在用户模型。同样,除非您在主题模型中设置了
belongs_to :user
,否则@topic.user
将不起作用。Are you sure there is no
has_many
in the user model@user.topics
will definitely not work unless you have thehas_many :topics
set in the User model.Just the same way
@topic.user
will not work unless you have set thebelongs_to :user
in the Topic model.