I'm new to ActiveRecord transactions. In the code below the first update_attributes causes a WARNING: Can't mass-assign protected attributes: account_type_cdx and that is ok. But I was surprised that the next line self.update_attributes!(:purchased => true) is executed and stored in the DB. I was expecting it to ROLLBACK because the first one failed.
I must be missing something... Any hints?
def complete_purchase(current_user_id, plan_name)
Rails.logger.debug "COMPLETE PURCHASE"
user = User.find(current_user_id)
ActiveRecord::Base.transaction do
user.update_attributes!(:account_type_cdx => plan_name.to_sym)
self.update_attributes!(:purchased => true)
end
end
I followed the advices from this post: http://markdaggett.com/blog/2011/12/01/transactions-in-rails/
Thanks.
Rails ignores those records that are not explicitly listed in attr_accessible
class call (hence the first update warning). It doesn't fail a transaction, that's why you'r reaching (and finishing) the second update_attributes! normally.