为什么 User 模型不需要 has_many?

发布于 2024-12-11 19:01:40 字数 388 浏览 2 评论 0原文

在“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 技术交流群。

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

发布评论

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

评论(1

你对谁都笑 2024-12-18 19:01:40

你确定用户模型中没有 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 the has_many :topics set in the User model.

Just the same way @topic.user will not work unless you have set the belongs_to :user in the Topic model.

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