Ecto has

编程入门 行业动态 更新时间:2024-10-08 10:58:32
本文介绍了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

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

发布评论

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

>www.elefans.com

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