Ah yes--I see that thread now--thanks!
I'm on an airplane & can't this minute check out the plugin Brandon recommends,
so I figured I'd try out the module approach you mention & am having trouble.
I've got a file /lib/model_helper.rb w/the following contents:
module ModelHelper
def pretend_helper(msg)
puts(msg)
end
end
And a model in care_phase.rb that starts out like:
class CarePhase < ActiveRecord::Base
include ModelHelper
pretend_helper("hi!")
If I then open the console & type CarePhase.new I get this here:
Loading development environment (Rails 2.0.2)
>> CarePhase.new
NoMethodError: undefined method `pretend_helper' for CarePhase(id: integer,
name: string):Class
from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in
`method_missing'
from C:/railsapps/collabtrac/app/models/care_phase.rb:9
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
`load_without_new_constant_marking'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
`load_file'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in
`new_constants_in'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in
`load_file'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in
`require_or_load'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in
`load_missing_constant'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in
`const_missing'
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in
`const_missing'
from (irb):1
Can anybody say what I'm doing wrong?
Thanks!
-Roy
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Joshua Abbott
Sent: Wednesday, January 14, 2009 9:18 PM
To: [email protected]
Subject: [Rails] Re: helper for model?
I just posted a similar question regarding best practices when you have methods
you want to share across multiple AR models.
I ended up writing a module and sticking it in the lib directory. In order to
make these methods available to your models, simply include it
with:
class Post < ActiveRecord::Base
include MyModule
end
That seems to be a very neat and clean way to juggle these methods and DRY up
your model code somewhat.
-- Josh
http://iammrjoshua.com
Brandon Keepers wrote:
> On Wed, Jan 14, 2009 at 8:26 PM, Pardee, Roy <[email protected]> wrote:
>> kid.attributes = atts
>> I'd like to write these methods once generically & call the generic versions
>> from the various models where I need to do this. But it seems that helper
>> methods aren't visible from within a model (right?). Where can I put these
>> generic methods so that they're visible from my models? I'm using rails
>> 2.0.2.
> You may want to check out the attribute_fu plugin:
> http://github.com/giraffesoft/attribute_fu/commits/master
>
> This wraps up this pattern cleaner than I could in my vain attempts.
> It also provides some view helpers for displaying the models.
>
> -- Brandon
> ----------------------------------------------------------------------
> ---------- Training by Collective Idea: Ruby on Rails training in a
> vacation setting http://training.collectiveidea.com San Antonio, TX
> Jan 20-23
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---