Attributes passed to .build() dont show up in the query

Posted by Sebastian on Stack Overflow See other posts from Stack Overflow or by Sebastian
Published on 2010-12-27T14:50:43Z Indexed on 2010/12/27 14:54 UTC
Read the original article Hit count: 194

Hi there guys! Hope your all enjoying your hollydays.

Ive run into a pretty funny problem when trying to insert rows into a really really simple database table.
The basic idea is pretty simple. The user selects one/multiple users in a multiselect which are supposed to be added to a group.

This piece of code will insert a row into the user_group_relationships table, but the users id always

   @group = Group.find(params[:group_id])

   params[:newMember][:users].each do |uid|
      # For debugging purposes.
      puts 'Uid:'+uid

      @rel = @group.user_group_relationships.build( :user_id => uid.to_i )          
      @rel.save 

    end

The user id always gets inserted as null even though it is clearly there. You can see the uid in this example is 5, so it should work.

Uid:5
...
SQL (0.3ms) INSERT INTO "user_group_relationships" ("created_at", "group_id", "updated_at", "user_id") VALUES ('2010-12-27 14:03:24.331303', 2, '2010-12-27 14:03:24.331303', NULL)

Any ideas why this fails?

© Stack Overflow or respective owner

Related posts about activerecord

Related posts about ruby-on-rails3