← Back to Upcase

Strange behavior when updating HABTM association


(Oto Hlincik) #1

I am running into a rather strange problem with trying to update a record that specifies HABTM through checkboxes. I searched for this on the web but could not find anything even close to my problem. Chances are that it could be related to using Rails 4.1.1 but I thought that I’d try my luck here.

I am populating checkboxes for the association in the form using the collection_check_boxes form helper and it appears to be working fine.

After I submit the form, I see that the IDs specified through the check boxes are submitted fine:

From Rails Server Log: “service_branch_ids”=>[“1”, “3”, “5”, “6”, “”]

I made sure that the IDs are permitted in strong parameters.

The actual update does something rather strange and I can’t pinpoint what is happening. Here is what the server log spits out:

21:42:42 web.1 | (0.3ms) BEGIN
21:42:42 web.1 | ServiceBranch Load (0.4ms) SELECT service_branches.* FROM service_branches WHERE service_branches.id IN (1, 3, 5, 6)
21:42:42 web.1 | ServiceBranch Load (0.5ms) SELECT service_branches.* FROM service_branches INNER JOIN photos_service_branches ON service_branches.id = photos_service_branches.service_branch_id WHERE photos_service_branches.photo_id = 110
21:42:42 web.1 | SQL (0.4ms) INSERT INTO photos_service_branches (photo_id, service_branch_id) VALUES (110, 6)
21:42:42 web.1 | (30.8ms) ROLLBACK
21:42:42 web.1 | (0.2ms) BEGIN
21:42:42 web.1 | SQL (0.4ms) UPDATE photos SET updated_at = ‘2014-06-27 01:42:42’, veteran_name = ‘Peter Tester’ WHERE photos.id = 110
21:42:42 web.1 | SQL (0.2ms) INSERT INTO photos_service_branches VALUES ()
21:42:42 web.1 | (0.7ms) COMMIT

I bolded the activity messages and highlighted the 2 strange parts. Apparently, the query to insert the correct ID into the intersection table is performed but then, rolled back. Subsequently, nothing is inserted into the intersection table instead (it actually inserts a row with NULL values for both foreign key IDs in the table).

Any insight would be appreciated. Perhaps, there is a way for me to extract some more data about why the transaction is being rolled back but I’m not entirely sure about how to do that.


(Pedro Moreira) #2

Can you post some more code for this, specially the strong params in your controller?


(Oto Hlincik) #3

Hey Pedro. Thanks for responding. Here is more code…

From the view:

<div class="form-group">
  <%= f.label :service_branch_ids, 'Branch of Service' %><br>
  <%= f.collection_check_boxes :service_branch_ids, ServiceBranch.all, :id, :branch, class: 'form-control input-box' do |cb|
        cb.label(class: 'checkbox-inline') { cb.check_box + cb.text }
      end
  %>
</div>

From the controller:

private

def photo_params
  params[:photo].permit(:submitter_name, :veteran_name, :photo_of, :service_entry_date_id,
    :utilized_services, service_branch_ids: [])
end

and

def update
  @photo.update(photo_params)
  @photo.save(validate: false)
  redirect_to session[:cancel_path]
end

From the server log:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "photo"=>{"submitter_name"=>"Jack Rabbit", "veteran_name"=>"Peter Tester", "photo_of"=>"Relation", "service_entry_date_id"=>"1", "utilized_services"=>"true", "service_branch_ids"=>["1", "3", "5", "6", ""]}, "button"=>"", "id"=>"110"}

Hope this is helpful.