Prakash Murthy
Prakash Murthy

Reputation: 13077

ActiveRecord::Persistence#update ignoring one attribute

I am trying to update a couple of attributes in my ticket model, but am stumped as the ActiveRecord::Persistence#update method ignores one of the attributes, and updates the other attributes passed in the hash.

ticket_params = {"total_quantity"=>"0", "status"=>"refunded"}
ticket.update(ticket_params)

Generates the following update query:

UPDATE `tickets` SET `status` = 'refunded', `updated_at` = '2015-03-20 21:01:48.145401' WHERE `tickets`.`id` = 307865

Same behavior happens when I have 3 attributes in the hash.

ticket_params = {"total_quantity"=>0, "status"=>"refunded", billing_name: "TT"}
t.update(ticket_params)

The generated update query:

UPDATE `tickets` SET `status` = 'paid', `billing_name` = 'TT', `updated_at` = '2015-03-20 21:04:47.893160' WHERE `tickets`.`id` = 307865

All these params are marked as required and permitted. Can't find anything special about total_quantity attribute anywhere in the code or in the db.

From db/schema.rb : t.integer "total_quantity", limit: 4

UPDATE: Thanks to @creativereason for the comments below. Output while trying to update total_quantity directly:

ticket.total_quantity
> 5
ticket.total_quantity = 6
> 6
ticket.total_quantity
> 6
ticket.save
> true
ticket.total_quantity
> 5

FINAL UPDATE: There was a before_validation callback which was setting the total_quantity back to the original value.

Upvotes: 0

Views: 574

Answers (1)

creativereason
creativereason

Reputation: 1524

Double check to make sure you aren't using any callbacks... A before_save or similar callback might be keeping it set to the original value and you wouldn't see that message.

Upvotes: 3

Related Questions