# \<=\> comparison operator method undefined in AR class

**URL:** <https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575>\
**Category:** Rails\
**Created:** [September 16, 2007, 2:39am UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575 "2007-09-16T02:39:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 16, 2007, 2:39am UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575/1 "2007-09-16T02:39:33Z")

</div>

I have an active record class that looks like the following:

class TemplatePhotoSlot \< ActiveRecord::Base  
belongs\_to :template

# used in the autoflow photo best fit algorithm

def \<=\>(other)  
self.slot\_num \< other.slot\_num ? -1 : 1  
end

def area # gets area of the photo\_slot based on width and height  
width\*height  
end

end

For some reason, when I call “sort” on an array of these items it will  
work the first couple of times. Then, it will stop working and it tells  
me:

undefined method `\<=\>’ for #TemplatePhotoSlot:0x4932d00

anybody have any idea what might be happening?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 16, 2007, 4:00am UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575/2 "2007-09-16T04:00:32Z")

</div>

I commented out the “\<=\>” method and it turns out that its happening  
with “area” method now as well…

Anybody experience this behavior before where, when your server first  
boots up, the methods are working, and then all of a sudden they stop?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575/3 "2007-09-17T13:25:41Z")

</div>

I had the same behavior with acts\_as\_commentable. Acts\_as\_commentable  
belongs\_to :user, and I could not access any methods I had defined in  
User after the first request. I solved it (thought I don’t know why) by  
moving comment.rb from /vendor/acts\_as\_commentable/lib to /app/models. I  
don’t know if this is related to your problem, but the behavior sounds  
awful similar.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 17, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575/4 "2007-09-17T21:18:14Z")

</div>

Hmm interesting, could this be happening because Im using “extend” on an  
object, not the actual singleton class? If so how else can I extend an  
AR object with module methods?

William P. wrote:

> I had the same behavior with acts\_as\_commentable. Acts\_as\_commentable  
> belongs\_to :user, and I could not access any methods I had defined in  
> User after the first request. I solved it (thought I don’t know why) by  
> moving comment.rb from /vendor/acts\_as\_commentable/lib to /app/models. I  
> don’t know if this is related to your problem, but the behavior sounds  
> awful similar.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [September 21, 2007, 4:30am UTC](https://www.ruby-forum.com/t/comparison-operator-method-undefined-in-ar-class/114575/5 "2007-09-21T04:30:28Z")

</div>

Not really on point here but if slot\_num is something that already  
supports \<=\>, you can tidy up your method:

def \<=\>(other)  
self.slot\_num \<=\> other.slot\_num  
end

As for the original problem, are you working in the development  
environment? There are some odd issues that can creep into  
development that have primarily to do with which classes are cached  
and which are reloaded. The good news is that they don’t (typically)  
effect production code. To feel safe, it’d probably be worth building  
some good unit tests.

On Sep 15, 8:39 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
