Forum: Ruby on Rails Mixins?

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.
Sean B. (Guest)
on 2006-04-11 15:47
(Received via mailing list)
As is often the case when I tackle a new platform/language, I get the
big
picture very quickly (because frameworks are frameworks are frameworks)
but
its the nitty-gritty of the language that bogs me down...

So I have some similar methods on a few of my model classes that I
wanted to
push into a helper. Now I reckoned that the Ruby way was to create a
module
and mix it in with include. However, I can't get it to work
<frustration>

In the first instance I put it straight into the models directory:

module FieldHelper
  def validate_field
    if template_field.mandatory? && (!field_value ||
field_value.length==0)
      errors.add(template_field.name, _('cant.be.blank'))
    end
  end
end

Now in the model I have this :

require "field_helper"

class SchemeAccountField < ActiveRecord::Base
  include FieldHelper

  protected
  def validate
    validate_field
  end

end

So basically I want to mix in very similar validation rules.

However, what I get is:

undefined local variable or method `validate_field' for ...

I'm obviously missing something obvious and/or being stupid but that's
the
nature of the game with a new language I suppose.

Any help out there?
Sean B. (Guest)
on 2006-04-11 18:15
(Received via mailing list)
OK sorted it. I forgot to put the module in a file in the lib
directory...
missed that bit. I can now include it in my model classes.
This topic is locked and can not be replied to.