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-extending-modules-in-ruby/

-
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
<rails-mailing-l...@andreas-s.net> wrote:
>
> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to