<=> comparison operator method undefined in AR class

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?

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?

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.

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.

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. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs