新手问题 联结表怎么更新额外的字段

miserytan · August 01, 2017 · Last by miserytan replied at August 02, 2017 · 1235 hits

两个表 users 和 roles,现有中间表 users_roles,我想更新中间表中的 name,name 由我自己定义,该怎么操作

@user.roles.update_attributes(:name => 'asd')

是这样的吗,我这个试过之后不好用啊?

补充说明:多对多关联使用的是 has_many :through

UsersRoles.where(user: @user).update(name: 'asd')

如果是用 form 的话,可以用这种 https://stackoverflow.com/a/23497023/2477886

链接表不应该有额外字段,id 都不需要,否则只是一个普通表。

问题已经得到解决,谢谢各位给出的建议,其实在 save 之后更新一下就好了,我非要在 save 前更新,搞得很糟

You need to Sign in before reply, if you don't have an account, please Sign up first.