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.