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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Friday 17 September 2010, Almog Friedman wrote:> 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.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 Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
the error i get is: NameError: uninitialized constant Test::ModelHelper On Fri, Sep 17, 2010 at 7:08 PM, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org>wrote:> On Friday 17 September 2010, Almog Friedman wrote: > > 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. > > 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 Schuerig > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org > http://www.schuerig.de/michael/ > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I am having the same issue... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2011-Jan-28 09:17 UTC
Re: including a module in my model rails 3
you need to require it in rails 3 or add "lib" directory to autoload paths Robert pankowecki -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.