# How to compare two model instances? is "m1.to\_yaml.eql?(m2.to\_yaml)" a good way?

**URL:** <https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989>\
**Category:** Rails\
**Created:** [September 5, 2009, 10:35am UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989 "2009-09-05T10:35:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 10:35am UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989/1 "2009-09-05T10:35:05Z")

</div>

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/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989/2 "2009-09-05T12:55:34Z")

</div>

On Sep 5, 9:30 am, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989/3 "2009-09-05T13:11:22Z")

</div>

2009/9/5 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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?

tks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989/4 "2009-09-05T23:59:31Z")

</div>

2009/9/6 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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…

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/how-to-compare-two-model-instances-is-m1-to-yaml-eql-m2-to-yaml-a-good-way/174989/5 "2009-09-05T19:31:59Z")

</div>

On Sep 5, 12:09 pm, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 2009/9/5 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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
