0

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.

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.

Anyway, as I said, when I PUT an update, for example to the threshold values, everything works ok. This is the controller code (UserGroupController, I am posting whole user groups rather than one user at a time):

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. The "Tallmaris" user works ok and the changed values are updated no problem (I can see the UPDATE sql getting run by the server); 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.

Tallmaris
  • 7,605
  • 3
  • 28
  • 58

1 Answers1

0

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.

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. :)

Community
  • 1
  • 1
Tallmaris
  • 7,605
  • 3
  • 28
  • 58