i have a function that i wanna use in different models and i wanna do it
DRY.
so my best option as i see it is to include a module i made (that is in
the
lib/ directory) in the model.
my code basicly looks something like this (for testing purpose, i didn’t
implement my function yet)
lib/model_helper.rb
module ModelHelper
def bla
puts “hello world”
end
end
app/models/test.rb
class Test < ActiveRecord::Base
include ModelHelper
end
now the weird part is, when i try to do this in rails 2.2 it works but
when
trying to do this in rails 3.0.0 it doesnt.
why is that?
is there any way of doing it in the newest version of rails?
On Friday 17 September 2010, Almog Friedman wrote:
end
end
app/models/test.rb
class Test < ActiveRecord::Base
include ModelHelper
end
now the weird part is, when i try to do this in rails 2.2 it works
but when trying to do this in rails 3.0.0 it doesnt.
It ought to work in both versions just the same. You may need to be more
specific what “not work” means. Also, it is relevant where that module
is located. Rails 1.x and 2.x automatically load files from lib, rails
3.x does not. So if your ModelHelper is in lib/model_helper.rb you need
to require that file explicitly where you use it.
If the module is tied to your application anyway, I’d put it in
app/models. In my opinion, app/models is not only for subclasses of
ActiveRecord::Base, rather, it is the place where the model layer of an
app resides.
Michael
–
Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/
the error i get is:
NameError: uninitialized constant Test::ModelHelper
you need to require it in rails 3 or add “lib” directory to autoload
paths
Robert pankowecki
I am having the same issue…