Forum: Ruby on Rails Is this model ordering logical or crazy?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ashley M. (Guest)
on 2006-04-06 02:26
(Received via mailing list)
Bit of a mind-bender here...

I'm got a model that tracks rules that are applied to different
levels of product data, including:
Make, Product Range, Model, Derivative.  There's a RuleLevel class to
represent each level and has a column position.  It looks something
like this:
class RuleLevel < ActiveRecord::Base
   include Comparable

   def +(offset)
     RuleLevel.level_for_position(position + offset)
   end

   def -(offset)
     RuleLevel.level_for_position(position - offset)
   end

   def <=>(other)
     return -(position <=> other.position)
   end

   private
     def self.level_for_position(position)
       StockRuleLevel.find(:first, :conditions => "position = #
{position}")
     end
end

Imagine I also have constants MAKE, RANGE, MODEL, DERIVATIVE in the
class to get at the levels with positions 1, 2, 3, 4 respectively.

Now it makes sense that a more general rule is "greater" than a more
specfic rule, or in Ruby:
RuleLevel.RANGE > RuleLevel.DERIVATIVE => true (1)

And it also makes sense that the "next" rule level is the next most
specific one (because they are always processed from the top down), ie:
RuleLevel.MAKE + 1 => RuleLevel.RANGE (2)

But this leads to the following bizarre inequality:
RuleLevel.MAKE > RuleLevel.MAKE + 1 => true (3)


Does this make sense?  It seems strange that an object should be less
than something increased by it, but if you change either (1) or (2)
the logic in the application sounds back to front.  Maybe someone
with better understanding of algebra can make sense of this (or
perhaps just someone who has slept more than 6 hours a night this
week!!!)

Gold star to anyone who can convince me one way or another ;)

Cheers
Ashley
This topic is locked and can not be replied to.