简体   繁体   English

多对多关系中的accept_nested_attributes_for

[英]accept_nested_attributes_for in a many-to-many relationship

I have tried to find a solution for this but most of the literature around involves how to create the form rather than how to save the stuff in the DB. 我试图为此找到一种解决方案,但是周围的大多数文献都涉及如何创建表格,而不是如何将内容保存在数据库中。 The problem I am having is that the accepts_nested_attributes_for seems to work ok when saving modifications to existing DB entities, but fails when trying to create a new object tree. 我遇到的问题是,将修改保存到现有数据库实体时, accepts_nested_attributes_for似乎可以正常工作,但是在尝试创建新的对象树时失败。

Some background. 一些背景。 My classes are as follows: 我的课程如下:

class UserGroup < ActiveRecord::Base
    has_many :permissions
    has_many :users

    accepts_nested_attributes_for :users
    accepts_nested_attributes_for :permissions
end

class User < ActiveRecord::Base
    has_many :user_permissions
    has_many :permissions, :through => :user_permissions

    belongs_to :user_group

    accepts_nested_attributes_for :user_permissions
end

class Permission < ActiveRecord::Base
    has_many :user_permissions
    has_many :users, :through => :user_permissions

    belongs_to :user_group
end

class UserPermission < ActiveRecord::Base
    belongs_to :user
    belongs_to :permission

    validates_associated :user
    validates_associated :permission

    validates_numericality_of :threshold_value
    validates_presence_of :threshold_value

    default_scope order("permission_id ASC")
end

The permission seem strange but each of them has a threshold_value which is different for each user, that's why it is needed like this. 权限看起来很奇怪,但是每个权限都有一个threshold_value,每个用户的阈值都不同,这就是为什么需要这样的原因。

Anyway, as I said, when I PUT an update, for example to the threshold values, everything works ok. 无论如何,正如我所说,当我PUT更新,例如到阈值,一切工作正常。 This is the controller code (UserGroupController, I am posting whole user groups rather than one user at a time): 这是控制器代码(UserGroupController,我一次发布整个用户组,而不是一次发布一个用户):

def update
    @ug = UserGroup.find(params[:id])
    @ug.update_attributes!(params[:user_group])
    respond_with @ug
end

A typical data coming in would be: 输入的典型数据为:

{"user_group":
    {"id":3,
    "permissions":[
        {"id":14,"name":"Perm1"},
        {"id":15,"name":"Perm2"}],
    "users":[
        {"id":7,"name":"Tallmaris",
         "user_permissions":[
             {"id":1,"permission_id":14,"threshold_value":"0.1"},       
             {"id":2,"permission_id":15,"threshold_value":0.3}]
        },
        {"name":"New User",
         "user_permissions":[
             {"permission_id":14,"threshold_value":0.4},
             {"permission_id":15,"threshold_value":0.2}]
        }]
    }
}

As you can see, the "New User" has no ID and his permission records have no ID either, because I want everything to be created. 如您所见,“新用户”没有ID,而他的权限记录也没有ID,因为我希望创建所有内容。 The "Tallmaris" user works ok and the changed values are updated no problem (I can see the UPDATE sql getting run by the server); “ Tallmaris”用户可以正常工作,并且更改后的值可以更新(我可以看到服务器上正在运行UPDATE sql); on the contrary, the new user gives me this nasty log: 相反,新用户给了我这个讨厌的日志:

[...]
User Exists (0.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."name" = 'New User' LIMIT 1
ModelSector Load (8.7ms)  SELECT "user_permissions".* FROM "user_permissions" WHERE (user_id = ) ORDER BY permission_id ASC
PG::Error: ERROR:  syntax error at or near ")"

The error is obviously the (user_id = ) with nothing, since of course the user does not exists, there are no user_permissions set already and I wanted them to be created on the spot. 错误显然是(user_id = )没有任何错误,因为用户当然不存在,还没有设置user_permissions,我希望当场创建它们。

Thanks to looking around to this other question I realised it was a problem with the validation on the user. 多亏环顾了另一个问题,我才意识到这是用户验证的问题。 Basically I was validating that the threshold_values summed up within certain constraints but to do that I was probably doing something wrong and Rails was loading data from the DB, which was ok for existing values but of course there was nothing for new values. 基本上,我正在验证threshold_values在某些约束条件下的总和,但是这样做可能是我做错了,Rails正在从DB加载数据,这对于现有值没问题,但是对于新值当然没有任何帮助。

I fixed that and now it's working. 我已经解决了,现在可以了。 I'll leave this here just as a reminder that often a problem in one spot has solutions coming from other places. 我将其留在此处只是为了提醒您,通常一个地方的问题有来自其他地方的解决方案。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM