Forum: Ruby on Rails Adding custom validations to active record

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.
7db3b1680807d393d40260591b4ae5e0?d=identicon&s=25 sa 125 (sa125)
on 2009-02-22 10:24
Hi - I want to add my own validation to a rails app, and need help
making it global to my app. Basically, the validation makes sure a
number is positive - I found this code on the at
http://snippets.dzone.com/posts/show/822:

module CustomValidations

  def validates_positive_or_zero(*attr_names)
    configuration = { :message => "Cannot be negative" }
    configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
    validates_each attr_names do |m, a, v|
      m.errors.add(a, configuration[:message]) if v<0
    end
  end

end

I want to use it in my models like any other validation:

class SomeModel < ActiveRecord::Base
  validates_positivity_of :some_number, :another_number
end

I saved it in lib/custom_validations.rb, and tried including it in the
top of my environment.rb to extend AR:

include 'custom_validations'
class ActiveRecord::Base
  include CustomValidations
end

#.. rest of file

Sadly, this won't work. How do I make rails identify this module and use
it properly?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-02-22 10:31
(Received via mailing list)
sa 125 wrote:

> Sadly, this won't work.
Always include even a snip of the error message you got. After your
detailed
writeup, just claiming "won't work" leaves us bereft of closure!

However, the top of environment.rb might be before "require
'active_record'".
That means 'class' would introduce 'ActiveRecord', leaving 'Base' as a
missing
identifier. If so, move it to the bottom!

--
   Phlip
7db3b1680807d393d40260591b4ae5e0?d=identicon&s=25 sa 125 (sa125)
on 2009-02-22 10:44
my bad. I moved that block to the bottom of the environment.rb file,
after the "end" of the Rails::Initializer code, but got this error:

NoMethodError (undefined method 'validates_positivity_of' for
#<Class...>)
   /../active_record/base.rb:1833:in 'method missing'

I tried loading this method though ./script/console:
irb> include CustomValidations
irb> validates_positivity_of 2
NoMethodError: undefined method 'validates_each' for #<object..>
   from /../lib/custom_validations.rb:6:in 'validates_positivity_of'

Now I'm not even sure if the function works properly.
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-22 12:56
(Received via mailing list)
It isn`t include in your case, it`s extend:

include 'custom_validations'
class ActiveRecord::Base
 extend CustomValidations
end

Here's a better explanation why ->
http://blog.codevader.com/2008/09/27/including-and...

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Sun, Feb 22, 2009 at 6:24 AM, sa 125
This topic is locked and can not be replied to.