Monday, October 3, 2011

Re: [Rails] Re: Aggregated attribute changed but _was is changed too. So nothing is update in database.

On Mon, Oct 3, 2011 at 9:56 PM, Romain Maz BILLOIR <billoirr@gmail.com> wrote:
This issue is making me crazy... Look at this:

ruby-1.9.3-rc1 :071 > @hunting = Hunting.find(1)
 Hunting Load (0.5ms)  SELECT "huntings".* FROM "huntings" WHERE
"huntings"."id" = $1 LIMIT 1  [["id", 1]]
 => #<Hunting id: 1, path: "((7,6),(5,4))", created_at: "2011-10-02
16:02:11", updated_at: "2011-10-03 19:16:44">
ruby-1.9.3-rc1 :072 > @hunting.path
 => ((7,6),(5,4))
ruby-1.9.3-rc1 :073 > @hunting.changed?
 => false
ruby-1.9.3-rc1 :074 > @hunting.changes
 => {}
ruby-1.9.3-rc1 :075 > @hunting.one_step_forward
 => (7,6)
ruby-1.9.3-rc1 :076 > @hunting.path
 => ((5,4))
ruby-1.9.3-rc1 :077 > @hunting.changed?
 => true
ruby-1.9.3-rc1 :078 > @hunting.changes
 => {"path"=>[((5,4)), ((5,4))]}

Strange, I would expect here:
 => {"path"=>["((7,6),(5,4))", "((5,4))"]}
 
ruby-1.9.3-rc1 :079 > @hunting.changed_attributes
 => {"path"=>((5,4))}

Strange, I would expect here:
=> {"path"=>"((7,6),(5,4))"}
  
ruby-1.9.3-rc1 :080 > @hunting.save
  (0.2ms)  BEGIN
  (0.5ms)  UPDATE "huntings" SET "path" = '((7,6),(5,4))',
"updated_at" = '2011-10-03 19:30:15.467748' WHERE "huntings"."id" = 1
  (23.5ms)  COMMIT
 => true


What happens, when on line :075 of your example above, you
simply say 

 @hunting.path '((5,4))'

instead of the call to Hunting#one_step_forward and replay the rest of your
example. Does it behave as expected then?

Are there any before_save hooks at play in your Hunting model?

Actually, I tried to replay your example (using a simple string assignment)
in Rails 3.1.1.rc1 and found the different results for .changes and
.changed_attributes after the assignment, mentioned above.

004:0> j = Job.find(1)
  Job Load (0.5ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = $1 LIMIT 1  [["id", 1]]
=> #<Job id: 1, created_at: "2011-10-03 20:56:27", updated_at: "2011-10-03 21:02:56", name: "foo", path: "((7,6),(5,4))">
005:0> j.path
=> "((7,6),(5,4))"
006:0> j.changed?
=> false
007:0> j.path = "((5,4))"
=> "((5,4))"
008:0> j.changed?
=> true
009:0> j.changes # this is different from your result and what I expected
=> {"path"=>["((7,6),(5,4))", "((5,4))"]}
010:0> j.changed_attributes # this is the original value, as documented
=> {"path"=>"((7,6),(5,4))"}
011:0> j.path_was
=> "((7,6),(5,4))"
012:0> j.path
=> "((5,4))"
013:0> j.save
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE "jobs" SET "path" = '((5,4))', "updated_at" = '2011-10-03 21:06:21.336787' WHERE "jobs"."id" = 1
   (0.7ms)  COMMIT
=> true
014:0> j = Job.find(1)
  Job Load (0.4ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = $1 LIMIT 1  [["id", 1]]
=> #<Job id: 1, created_at: "2011-10-03 20:56:27", updated_at: "2011-10-03 21:06:21", name: "foo", path: "((5,4))">

HTH,

Peter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment