Mixins?


#1

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

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?


#2

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.