通过构建的has

编程入门 行业动态 更新时间:2024-10-08 04:24:28
本文介绍了通过构建的has_many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个型号。用户和帐户如下:

I have two models. User and Account as follows

class Account < ActiveRecord::Base has_many :manages has_many :users, :through => :manages end class User < ActiveRecord::Base has_many :manages has_many :accounts, :through => :manages end

如果我是用钢轨控制台和

If I were to use the rails console and create an instance of account by

acc = usr.accounts.build acc.save

下面的命令将返回创建的帐户实例

The following command would return the account instance created

usr.accounts

但下面的命令不会返回用户实例

But the following command would not return the user instance

acc.users

此外,当我看着桌子加入,也没有创建条目。什么是缺少在这里?我想用它自动创建连接模型条目构建方法。

Also when I look at the Join table, there is no entry created. What am missing here? I thought by using the build method that it automatically creates the join model entry.

推荐答案

尝试节省了用户的对象,而不是

Try saving the user object instead.

acc = usr.accounts.build usr.save

更多推荐

通过构建的has

本文发布于:2023-08-03 14:45:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!