Rails 3.2.5 Regression: incorrect _changed? for datetimes #6591

Closed
earnold opened this Issue Jun 1, 2012 · 21 comments
@earnold

I just upgraded from 3.2.2 to 3.2.5, and now I have failing specs. I've identified the problem as this.

When you have this model:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.datetime "published_at"
  end

previously you could do:

1.9.3p125 :013 >p = Post.last
1.9.3p125 :014 > p.published_at = p.published_at
1.9.3p125 :015 > p.changed?
=> false 

but now:

1.9.3p125 :013 >p = Post.last
1.9.3p125 :014 > p.published_at = p.published_at
1.9.3p125 :015 > p.changed?
=> true 
1.9.3p125 :006 > p.changes
 => {"published_at"=>[Thu, 12 Apr 2012 19:32:33 UTC +00:00, Thu, 12 Apr 2012 19:32:33 UTC +00:00]} 

I'm going to see if I can put together a failing test around this.

@carlosantoniodasilva
Ruby on Rails member

Just a side question: have you upgraded straight from 3.2.2 to 3.2.5? If so, can you try 3.2.3 as well to check the issue? Thanks.

@earnold

I just confirmed that this works correctly with 3.2.3, so it is a new problem

@carlosantoniodasilva
Ruby on Rails member

Great, we'll investigate, thanks.

@earnold

Here is a failing spec: earnold@5f27132

@kennyj

Please see #3965 and ddb5d2f .
I think they are related to this issue.

@kennyj

All tests are passed if we add the above commit.
But I don't realize deeply. Tomorrow, I'll read #3965 more :-)

@kennyj

cc/ @lest

@lest

Datetime attributes was forced to be changed in #5196 because of incorrect dirty tracking described in #3965. Then it was backported to the 3-2-stable branch in #5618.

@kennyj Your fix seems good to me.

@kennyj

I read #3965. and I also do not think the my fix was bad.
I'll submit PR to master and 3-2-stable.

Thanks @lest for reviewing ! :-)

@kennyj kennyj added a commit to kennyj/rails that referenced this issue Jun 4, 2012
@kennyj kennyj Fix #6591. If a field with timezone isn't changed, we don't call will…
…_change!.
635032f
@kennyj

I submit PR.
See #6619 .

@kennyj kennyj closed this in 635032f Jun 4, 2012
@carlosantoniodasilva
Ruby on Rails member

Hey guys, thank you for fixing this :)

@kennyj kennyj added a commit to kennyj/rails that referenced this issue Jun 4, 2012
@kennyj kennyj Fix #6591. If a field with timezone isn't changed, we don't call will…
…_change!.
510ef2b
@pacoguzman

Hi guys, I've found a similar issue in current rails 3-2-stable. Adding the following test to activerecord/test/cases/dirty_test.rb

  def test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change
    in_time_zone 'Paris' do
      target = Class.new(ActiveRecord::Base)
      target.table_name = 'pirates'

      created_on = Time.now

      pirate = target.create(:created_on => created_on)
      pirate.reload # Here mysql truncate the usec value to 0

      pirate.created_on = created_on
      assert !pirate.created_on_changed?
    end
  end

I getting the following:

ARCONN=mysql2 ruby -Itest test/cases/dirty_test.rb -n test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_changeUsing mysql2 with Identity Map off
Run options: -n test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change

# Running tests:

F

Finished tests in 0.087599s, 11.4157 tests/s, 11.4157 assertions/s.

  1) Failure:
test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change(DirtyTest) [test/cases/dirty_test.rb:104]:
Failed assertion, no message given.

1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
ARCONN=sqlite3 ruby -Itest test/cases/dirty_test.rb -n test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change
Using sqlite3 with Identity Map off
Run options: -n test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change

# Running tests:

.

Finished tests in 0.058396s, 17.1245 tests/s, 17.1245 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

I have a clue in why this is happening but I don't know where is the right place to fix it or maybe this is not consider a bug.

Time.now.usec
 => 339426

Time.zone.local(2012, 02, 20, 9).usec
 => 0 

Thanks in advance

@rafaelfranca
Ruby on Rails member

@pacoguzman could you open an issue or a pull request with the failing test case?

@pixeltrix @kennyj could you guys take a look on this one?

@pacoguzman

@rafaelfranca of course, it's here #6975

@kennyj

@rafaelfranca @pacoguzman I'm taking a look on #6975.

@tinderfields

I'm getting this issue in 4.1.6 as well

@luxflux

Me too, Rails 4.1.11.

@mintuhouse

I am able to reproduce it on 4.1.13
using postgres and passing the time from browser as string in format "2015-12-30 4:58 PM"

@benbartling

I'm also having this issue. Rails 4.2.3

@maclover7
Ruby on Rails member

Is this an issue on master?

@maclover7 maclover7 reopened this Jan 19, 2016
@rafaelfranca
Ruby on Rails member

Until we have reproduction steps and confirmation that the issue is really present in recent versions we should keep it closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment