[Rails] Re: ActiveRecord Comparison Bug?
> Your case is different because of STI (so your comparisons here are > all false because the objects are of different class). If it weren't > for that then it's a slightly messed up situation - Active record > thinks it has a new record but it doesn't Doh! Didn't catch that. And when I do it without involving STI then I get the results he's getting below. Sorry Moo! Hrm... well down in AR::Base we have this: # Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id. def ==(comparison_object) comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && comparison_object.id == id && !comparison_object.new_record?) end Which interestingly enough matches each of the conditions the parent posted about. I guess what this means then is if you're mucking around setting the id's of a record and before they are saved you should call equal? directly. >> old = AdminUser.find(1) => # >> new = AdminUser.new => # >> new.id = 1 => 1 >> old == new => false >> new == old => true >> old.equal?(new) => false >> new.equal?(old) => false > Fred > >> old = Toy.fin>> old = Toy.find(1) >> => # >> >> new = Toy.new >> => # >> >> new.id = 1 >> => 1 >> >> old == new >> => false >> >> new == old >> => false >> >> Rails 2.3.2.1 >> >> -philip >> >>> appreciate any comments! >>> -Moo >> >>> On Apr 10, 10:52 am, Frederick Cheung >>> wrote: On Apr 10, 5:50 pm, Moo wrote:> Anyone has any thoughts on this please? >> Basically the rationale is that == should mean 'do these objects correspond to the same database row ?' >> The reason why unsaved records are special cased is that two unsaved record would have equal id (nil in both cases) but if you saved them you would end up with 2 distinct rows in your database. >> I'm not sure why you're getting new ==old not being the same as old == new. They should both be false (and are on my machine) >> Fred >> > On Apr 8, 5:29 pm, Moo wrote: >> >> Hi Everyone, >> >> I'm running into a problem with theActiveRecord::Base "==" method >> defined here (it's 2.3.2, but it looks the same in older versions >> too): >> >> # File rails-2.3.2/activerecord/lib/active_record/base.rb, >> line >> 2816 >> 2816: def ==(comparison_object) >> 2817: comparison_object.equal?(self) || >> 2818: (comparison_object.instance_of?(self.class) && >> 2819: comparison_object.id == id && >> 2820: !comparison_object.new_record?) >> 2821: end >> >> Because of the last criteria (!comparison_object.new_record?), >> something like this happens... say i have a new record and an >> existing >> record (different objects) and compare them: >> >>> new == old >> => true >>> old == new >> >> => false >> >> If this is intentional, can someone please explain why this is? >> >> Also what is the rational of only comparing the ID and not the >> all the >> values too and why does it matter if it's a new record or not? >> >> Thank you! >> -Moo > > --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: ActiveRecord Comparison Bug?
On Apr 16, 4:17 am, Philip Hallstrom wrote: > > You've got something screwy somewhere... > Your case is different because of STI (so your comparisons here are all false because the objects are of different class). If it weren't for that then it's a slightly messed up situation - Active record thinks it has a new record but it doesn't Fred > old = Toy.fin>> old = Toy.find(1) > => # > >> new = Toy.new > => # > >> new.id = 1 > => 1 > >> old == new > => false > >> new == old > => false > > Rails 2.3.2.1 > > -philip > > > appreciate any comments! > > -Moo > > > On Apr 10, 10:52 am, Frederick Cheung > > wrote: > >> On Apr 10, 5:50 pm, Moo wrote:> Anyone has any > >> thoughts on this please? > > >> Basically the rationale is that == should mean 'do these objects > >> correspond to the same database row ?' > > >> The reason why unsaved records are special cased is that two unsaved > >> record would have equal id (nil in both cases) but if you saved them > >> you would end up with 2 distinct rows in your database. > > >> I'm not sure why you're getting new ==old not being the same as old > >> == new. They should both be false (and are on my machine) > > >> Fred > > >>> On Apr 8, 5:29 pm, Moo wrote: > > Hi Everyone, > > I'm running into a problem with theActiveRecord::Base "==" method > defined here (it's 2.3.2, but it looks the same in older versions > too): > > # File rails-2.3.2/activerecord/lib/active_record/base.rb, > line > 2816 > 2816: def ==(comparison_object) > 2817: comparison_object.equal?(self) || > 2818: (comparison_object.instance_of?(self.class) && > 2819: comparison_object.id == id && > 2820: !comparison_object.new_record?) > 2821: end > > Because of the last criteria (!comparison_object.new_record?), > something like this happens... say i have a new record and an > existing > record (different objects) and compare them: > > > new == old > => true > > old == new > > => false > > If this is intentional, can someone please explain why this is? > > Also what is the rational of only comparing the ID and not the > all the > values too and why does it matter if it's a new record or not? > > Thank you! > -Moo --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: ActiveRecord Comparison Bug?
> Hi Fred, > > Thank you for the response! What you said made sense. thanks for the > explanation. > > However, looking at the last criteria of the == implementation, we can > see why something like what i mentioned would fail: > > let's say i have an 2 AR objects: >> old = Something.find(1) > => # > > then i create a new object: >> new = Something.new > => # >> new.id = 1 # something like this is possible, how or why is another >> story... >> new > => # > > now do: >> new == old > => true >> old == new > => false > > == method should be commutative, right? You've got something screwy somewhere... old = Toy.fin>> old = Toy.find(1) => # >> new = Toy.new => # >> new.id = 1 => 1 >> old == new => false >> new == old => false Rails 2.3.2.1 -philip > appreciate any comments! > -Moo > > On Apr 10, 10:52 am, Frederick Cheung > wrote: >> On Apr 10, 5:50 pm, Moo wrote:> Anyone has any >> thoughts on this please? >> >> Basically the rationale is that == should mean 'do these objects >> correspond to the same database row ?' >> >> The reason why unsaved records are special cased is that two unsaved >> record would have equal id (nil in both cases) but if you saved them >> you would end up with 2 distinct rows in your database. >> >> I'm not sure why you're getting new ==old not being the same as old >> == new. They should both be false (and are on my machine) >> >> Fred >> >>> On Apr 8, 5:29 pm, Moo wrote: >> Hi Everyone, >> I'm running into a problem with theActiveRecord::Base "==" method defined here (it's 2.3.2, but it looks the same in older versions too): >> # File rails-2.3.2/activerecord/lib/active_record/base.rb, line 2816 2816: def ==(comparison_object) 2817: comparison_object.equal?(self) || 2818: (comparison_object.instance_of?(self.class) && 2819: comparison_object.id == id && 2820: !comparison_object.new_record?) 2821: end >> Because of the last criteria (!comparison_object.new_record?), something like this happens... say i have a new record and an existing record (different objects) and compare them: >> > new == old => true > old == new >> => false >> If this is intentional, can someone please explain why this is? >> Also what is the rational of only comparing the ID and not the all the values too and why does it matter if it's a new record or not? >> Thank you! -Moo > > --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: ActiveRecord Comparison Bug?
Hi Fred, Thank you for the response! What you said made sense. thanks for the explanation. However, looking at the last criteria of the == implementation, we can see why something like what i mentioned would fail: let's say i have an 2 AR objects: > old = Something.find(1) => # then i create a new object: > new = Something.new => # > new.id = 1 # something like this is possible, how or why is another story... > new => # now do: > new == old => true > old == new => false == method should be commutative, right? appreciate any comments! -Moo On Apr 10, 10:52 am, Frederick Cheung wrote: > On Apr 10, 5:50 pm, Moo wrote:> Anyone has any thoughts > on this please? > > Basically the rationale is that == should mean 'do these objects > correspond to the same database row ?' > > The reason why unsaved records are special cased is that two unsaved > record would have equal id (nil in both cases) but if you saved them > you would end up with 2 distinct rows in your database. > > I'm not sure why you're getting new ==old not being the same as old > == new. They should both be false (and are on my machine) > > Fred > > > On Apr 8, 5:29 pm, Moo wrote: > > > > Hi Everyone, > > > > I'm running into a problem with theActiveRecord::Base "==" method > > > defined here (it's 2.3.2, but it looks the same in older versions > > > too): > > > > # File rails-2.3.2/activerecord/lib/active_record/base.rb, line > > > 2816 > > > 2816: def ==(comparison_object) > > > 2817: comparison_object.equal?(self) || > > > 2818: (comparison_object.instance_of?(self.class) && > > > 2819: comparison_object.id == id && > > > 2820: !comparison_object.new_record?) > > > 2821: end > > > > Because of the last criteria (!comparison_object.new_record?), > > > something like this happens... say i have a new record and an existing > > > record (different objects) and compare them: > > > > > new == old > > > => true > > > > old == new > > > > => false > > > > If this is intentional, can someone please explain why this is? > > > > Also what is the rational of only comparing the ID and not the all the > > > values too and why does it matter if it's a new record or not? > > > > Thank you! > > > -Moo --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: ActiveRecord Comparison Bug?
On Apr 10, 5:50 pm, Moo wrote: > Anyone has any thoughts on this please? > Basically the rationale is that == should mean 'do these objects correspond to the same database row ?' The reason why unsaved records are special cased is that two unsaved record would have equal id (nil in both cases) but if you saved them you would end up with 2 distinct rows in your database. I'm not sure why you're getting new ==old not being the same as old == new. They should both be false (and are on my machine) Fred > On Apr 8, 5:29 pm, Moo wrote: > > > Hi Everyone, > > > I'm running into a problem with the ActiveRecord::Base "==" method > > defined here (it's 2.3.2, but it looks the same in older versions > > too): > > > # File rails-2.3.2/activerecord/lib/active_record/base.rb, line > > 2816 > > 2816: def ==(comparison_object) > > 2817: comparison_object.equal?(self) || > > 2818: (comparison_object.instance_of?(self.class) && > > 2819: comparison_object.id == id && > > 2820: !comparison_object.new_record?) > > 2821: end > > > Because of the last criteria (!comparison_object.new_record?), > > something like this happens... say i have a new record and an existing > > record (different objects) and compare them: > > > > new == old > > => true > > > old == new > > > => false > > > If this is intentional, can someone please explain why this is? > > > Also what is the rational of only comparing the ID and not the all the > > values too and why does it matter if it's a new record or not? > > > Thank you! > > -Moo --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---
[Rails] Re: ActiveRecord Comparison Bug?
Anyone has any thoughts on this please? On Apr 8, 5:29 pm, Moo wrote: > Hi Everyone, > > I'm running into a problem with the ActiveRecord::Base "==" method > defined here (it's 2.3.2, but it looks the same in older versions > too): > > # File rails-2.3.2/activerecord/lib/active_record/base.rb, line > 2816 > 2816: def ==(comparison_object) > 2817: comparison_object.equal?(self) || > 2818: (comparison_object.instance_of?(self.class) && > 2819: comparison_object.id == id && > 2820: !comparison_object.new_record?) > 2821: end > > Because of the last criteria (!comparison_object.new_record?), > something like this happens... say i have a new record and an existing > record (different objects) and compare them: > > > new == old > => true > > old == new > > => false > > If this is intentional, can someone please explain why this is? > > Also what is the rational of only comparing the ID and not the all the > values too and why does it matter if it's a new record or not? > > Thank you! > -Moo --~--~-~--~~~---~--~~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~--~~~~--~~--~--~---