Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: .save does not update updated_at for existing records?

I always thought .save and .save! would update the updated_at column of existing records. Is this not true? If so, then do I need to create a before_save filter to update it everytime I save?

Today is June 18th:

    Loading development environment (Rails 4.1.1)
    irb(main):001:0>  o = Order.last
      Order Load (0.4ms)  SELECT  `orders`.* FROM `orders`   ORDER BY `orders`.`id` DESC LIMIT 1
    => #<Order id: 24, user_id: 1, order_date: nil, total_cents: 0, total_currency: "USD", shipping_cents: 0, shipping_currency: "USD", tax_cents: 0, tax_currency: "USD", subtotal_cents: 0, subtotal_currency: "USD", created_at: "2014-06-13 21:24:38", updated_at: "2014-06-13 21:24:38", status: "Incomplete", coupon_id: nil>
    irb(main):002:0> o.save!
       (0.4ms)  BEGIN
       (0.3ms)  COMMIT
    => true
    irb(main):003:0> o.updated_at
    => Fri, 13 Jun 2014 21:24:38 UTC +00:00
    irb(main):004:0> o.reload
      Order Load (0.7ms)  SELECT  `orders`.* FROM `orders`  WHERE `orders`.`id` = 24 LIMIT 1
    => #<Order id: 24, user_id: 1, order_date: nil, total_cents: 0, total_currency: "USD", shipping_cents: 0, shipping_currency: "USD", tax_cents: 0, tax_currency: "USD", subtotal_cents: 0, subtotal_currency: "USD", created_at: "2014-06-13 21:24:38", updated_at: "2014-06-13 21:24:38", status: "Incomplete", coupon_id: nil>
    irb(main):005:0> o.updated_at
    => Fri, 13 Jun 2014 21:24:38 UTC +00:00

The reason I care about this is because I am wrapping the internals of a function with ActiveRecord::Base.transaction so I want to test that they are not updated when something goes wrong:

Rspec test (that passes all the time when it's not supposed to, since updated_at isn't being changed regardless of success or failure):

  it "rollsback the transaction" do
    ...
    order.stub(:save!).and_raise(StandardError)
    expect { payment_info.save }.not_to change { [billing_address.updated_at, 
                                                  shipping_address.updated_at,
                                                  order.user.updated_at,
                                                  order.updated_at] }
  end

And my method:

def process_billing_info!
  ActiveRecord::Base.transaction do
    billing_address.save!
    shipping_address.save!
    user.save!
    order.update_all_fees!
  end
rescue Exception => e
  false
end
like image 587
bigpotato Avatar asked Jun 18 '14 16:06

bigpotato


1 Answers

As you can see in your log, there is no UPDATE SQL query that is executed. Rails is not updating your record at all. This is because .save actually saves the record only if changes were made.

There is the method .touch (Documentation) that you can call in order to update the updated_at field without having to do changes to your record:

1.9.3p489 :005 > Intervention.first.touch
  Intervention Load (12.9ms)  SELECT "interventions".* FROM "interventions" LIMIT 1
  SQL (20.5ms)  UPDATE "interventions" SET "updated_at" = '2014-06-18 16:34:03.924648' WHERE "interventions"."id" = 1
 => true 

Here we see the UPDATE SQL query.

like image 131
MrYoshiji Avatar answered Nov 15 '22 18:11

MrYoshiji