views:

167

answers:

2

First, let me say I am extremely new to Rails (toyed with it a time or two but forcing myself to write a complete project with it now, started on it yesterday).

I am now trying to validate that a model property (terminology?) is greater than another. This appeared to be a perfect instance for validates_numericality_of with the greater_than option, but alas that throws an error telling me greater_than expects a number, not a symbol. If I try to typecast that symbol .to_f I get an undefined method error.

Here is what I eventually did and I am curious as to whether there is a better way. It's just a simple system to control project releases, we only have major/minor releases (one-dot) so float felt like the right decision here.

class Project < ActiveRecord::Base
    validates_numericality_of :current_release
    validates_numericality_of :next_release
    validate :next_release_is_greater

    def next_release_is_greater
        errors.add_to_base("Next release must be greater than current release") unless next_release.to_f > current_release.to_f
    end
end

This works - it passes the relevant unit test (below for your viewing pleasure), I'm just curious as to if there is an easier way - something I could have tried otherwise.

Relevant unit test:

# Fixture data:
#   PALS:
#     name: PALS
#     description: This is the PALS project
#     current_release: 1.0
#     next_release: 2.0
#     project_category: 1
#     user: 1
def test_release_is_future
    project = Project.first(:conditions => {:name => 'PALS'})
    project.current_release = 10.0
    assert !project.save

    project.current_release = 1.0
    assert project.save
end
+1  A: 

That's the best way to execute custom validation, however, you might want to look into something like factory_girl as a replacement to fixtures (which it looks like you are using):

http://github.com/thoughtbot/factory%5Fgirl

Your unit test would then look like:

def test_...
    Factory.create(:project, :current_release => 10.0)
    assert !Factory.build(:project, :current_release => 1.0).valid?
end
jonnii
+1  A: 

As you noticed, the only way is to use a custom validator. The :greather_than option should be an integer. The following code won't work because both current and next release are available only at instance-level.

class Project < ActiveRecord::Base
  validates_numericality_of :current_release
  validates_numericality_of :next_release, :greather_than => current_release
end

The purpose of the greather_than option is to validate the value against a static constant or an other class method.

So, don't mind and go ahead with your custom validator. :)

Simone Carletti
Excellent answer, detailed response - just what I needed (plus a little reassurance).
Michael Wales