Greg Hauptmann
2009-Sep-05 08:30 UTC
how to compare two model instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way?
Hi, What''s an easy way to compare for equality two model instances (that haven''t been saved yet). Or in other words pretty much just two object instances. I''ve tried "==" and "eql?" on the objects directly and this doesn''t work. How about: "m1.to_yaml.eql?(m2.to_yaml)" Is this the simplest way? -- Greg http://blog.gregnet.org/
Frederick Cheung
2009-Sep-05 10:07 UTC
Re: how to compare two model instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way?
On Sep 5, 9:30 am, Greg Hauptmann <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > What''s an easy way to compare for equality two model instances (that > haven''t been saved yet). Or in other words pretty much just two > object instances. I''ve tried "==" and "eql?" on the objects directly > and this doesn''t work. > > How about: "m1.to_yaml.eql?(m2.to_yaml)" Is this the simplest way?Compare their attributes ? Fred> > -- > Greghttp://blog.gregnet.org/
Greg Hauptmann
2009-Sep-05 11:09 UTC
Re: how to compare two model instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way?
2009/9/5 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Compare their attributes ? > > FredGuess I was looking for an automated way to do this if one existed? or is there some concept of having to implement some basic comparison class in Ruby & then being able to have the "==" method work? tks
Frederick Cheung
2009-Sep-05 16:39 UTC
Re: how to compare two model instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way?
On Sep 5, 12:09 pm, Greg Hauptmann <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 2009/9/5 Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > Compare their attributes ? > > > Fred > > Guess I was looking for an automated way to do this if one existed? > or is there some concept of having to implement some basic comparison > class in Ruby & then being able to have the "==" method work? >Classes can implement their own == method. ActiveRecord::Base does do this, just not the way you want it too - ActiveRecord considers two objects to be equal if they correspond to the same row in the database (ie have the same id). If you have two unsaved objects then saving them would yield two different rows in the database, therefore by AR''s reckoning they are not equal. While you could override == on your class that may well break bits of ActiveRecord that expects == to behave in a certain way. Fred
Greg Hauptmann
2009-Sep-05 21:57 UTC
Re: how to compare two model instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way?
2009/9/6 Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Classes can implement their own == method. ActiveRecord::Base does do > this, just not the way you want it too - ActiveRecord considers two > objects to be equal if they correspond to the same row in the database > (ie have the same id). If you have two unsaved objects then saving > them would yield two different rows in the database, therefore by AR''s > reckoning they are not equal. While you could override == on your > class that may well break bits of ActiveRecord that expects == to > behave in a certain way.thanks for the advice Frederick - I think I''ll just implement my own "custom_eql?" method on my class then to be safe....