Forum: Ruby on Rails "Helper" function for my models

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.
25db3931245b6b6358c872c31db53f6d?d=identicon&s=25 Geoff Kloess (Guest)
on 2006-01-26 23:11
(Received via mailing list)
I'd like to create a validation function that I can use in all of my
models.  What's the best way to create a method that will be available
to all of my models?  It looks like Helper methods only work in Views
and methods in the Application class only work in controllers, so how
do I make one for models?

I hope this question isn't too lame, it seems like something I should
be able to find out on my own but I've looked through the book and
online and haven't been able to figure it out.

Thanks,
Geoff
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-27 00:21
(Received via mailing list)
On 1/26/06, Geoff Kloess <geoff.kloess@gmail.com> wrote:
> I'd like to create a validation function that I can use in all of my
> models.  What's the best way to create a method that will be available
> to all of my models?  It looks like Helper methods only work in Views
> and methods in the Application class only work in controllers, so how
> do I make one for models?
>
> I hope this question isn't too lame, it seems like something I should
> be able to find out on my own but I've looked through the book and
> online and haven't been able to figure it out.

If you put a file in the /lib/ folder, you can require it in your
model, or in environment.rb.
/your_rails_app/lib/example_helper.rb

require 'example_helper'
class Example < ActiveRecord::Base
  # stuff here.
end
This topic is locked and can not be replied to.