ActiveRecord::Base.attributes = {} incorrectly sets changed? for datetime values #7119

Closed
johvet opened this Issue Jul 20, 2012 · 5 comments

Comments

Projects
None yet
5 participants

johvet commented Jul 20, 2012

class CreatePirates < ActiveRecord::Migration
def change
create_table :pirates do |t|
t.datetime :published_at

  t.timestamps
end

end
end

class Pirate < ActiveRecord::Base
attr_accessible :published_at
end

In the rails console try this:

1.9.2p318 :002 > p = Pirate.last
Pirate Load (0.1ms) SELECT "pirates".* FROM "pirates" ORDER BY "pirates"."id" DESC LIMIT 1
=> #<Pirate id: 1, published_at: "2012-07-20 12:48:40", created_at: "2012-07-20 12:48:41", updated_at: "2012-07-20 12:48:41">
1.9.2p318 :003 > p.attributes = {published_at: p.published_at.to_s}
=> {:published_at=>"2012-07-20 12:48:40 UTC"}
1.9.2p318 :004 > p.changed?
=> true
1.9.2p318 :005 > p.changes
=> {"published_at"=>[Fri, 20 Jul 2012 12:48:40 UTC +00:00, Fri, 20 Jul 2012 12:48:40 UTC +00:00]}

As you can see, the record is marked as changed, even though the published value hasn't changed.

Where it seems to work fine for this case:

1.9.2p318 :016 > p.published_at = p.published_at
=> Fri, 20 Jul 2012 12:48:40 UTC +00:00
1.9.2p318 :017 > p.changed?

Owner

rafaelfranca commented Jul 20, 2012

Related with #6986

Member

senny commented Oct 28, 2012

I think this has been fixed. There is even a test-case:

https://github.com/rails/rails/blob/master/activerecord/test/cases/dirty_test.rb#L83

Owner

guilleiguaran commented Oct 28, 2012

Thanks, I'm closing this.

Feel free to re-open if still happening.

bpardee commented Oct 3, 2013

This still seems to be an issue in Rails 4 if you have a different timezone and parse from a string:

> a
=> #<Appointment id: 263982, start_at: "2013-08-26 18:00:00", end_at: "2013-08-26 18:30:00">
> a.start_at = a.start_at.to_s
=> "2013-08-26 18:00:00 UTC"
> a.changes
=> {}
> a.start_at = a.start_at.in_time_zone("Eastern Time (US & Canada)")
=> Mon, 26 Aug 2013 14:00:00 EDT -04:00
> a.changes
=> {}
> a.start_at = a.start_at.in_time_zone("Eastern Time (US & Canada)").to_s
=> "2013-08-26 14:00:00 -0400"
> a.changes
=> {"start_at"=>[Mon, 26 Aug 2013 18:00:00 UTC +00:00, Mon, 26 Aug 2013 18:00:00 UTC +00:00]}

bpardee commented Oct 7, 2013

Created a separate issue for my above comment (Issue # 12459)

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