Ecto has_many:通过表单 [英] Ecto has_many :through in form

查看:50
本文介绍了Ecto has_many:通过表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Ecto中建立has_many :through关系,以实现User模型和Group模型之间的许多关系.

I am trying to get a has_many :through relationship working in Ecto for a many-many relationship between a User model and Group model.

我只能在网上找到的唯一信息与JoséValim发表的帖子中的嵌套属性有关

The only information I was able to find online was related to nested attributes in a post by José Valim here (which is excellent, by the way).

由于组已经存在于系统中,所以我希望进行多选输入.我在这样做时遇到了几个问题.我不认为可以直接在变更集中分配groups关联,因为每次尝试这样做都会出错.我的下一个想法是手动完成工作(查找,删除和插入GroupMembership记录),但是我不确定这是否是正确的选择,并想首先获得一些建议.

Since the groups already exist in the system, I was hoping to do a multiple select input. I ran into a couple issues in doing that. I don't believe that it is possible to assign the groups association in the changeset directly because I was getting an error every time I attempted to do so. My next thought was to do the work manually (finding, deleting, and inserting GroupMembership records), however I wasn't sure if this was the right path to go down, and wanted to get some advice first.

由于代码示例有很多行,所以在此处.

Since the code sample has quite a few lines I made a gist here.

如果希望我直接将其发布在此问题中,我当然可以这样做.

If it's preferred that I post it in this question directly, I can certainly do that.

感谢所有帮助!

推荐答案

不幸的是,Ecto 1.0不支持多对多.这意味着您将需要接收ID并为要与用户关联的每个组手动建立中间关联.我们希望在将来的版本中使此操作变得更容易.

Unfortunately Ecto 1.0 does not support many to many. It means you will need to receive the IDs and manually build the intermediate association for each group you are associating to the user. We hope to make this easier in future releases.

Ecto 2.0支持many_to_many.

Ecto 2.0 supports many_to_many.

这篇关于Ecto has_many:通过表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆