# Helper for model?

**URL:** <https://www.ruby-forum.com/t/helper-for-model/157761>\
**Category:** Rails\
**Created:** [January 15, 2009, 2:27am UTC](https://www.ruby-forum.com/t/helper-for-model/157761 "2009-01-15T02:27:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 15, 2009, 2:27am UTC](https://www.ruby-forum.com/t/helper-for-model/157761/1 "2009-01-15T02:27:17Z")

</div>

Hey All,

I find I’m writing a ton of nearly identical model methods to support  
mass creation/update of child objects (a la the ‘complex forms’ series  
of railscasts). Stuff like:

def existing\_child\_attributes=(updated\_kids)  
children.each do |kid|  
unless kid.new\_record?  
atts = updated\_kids[kid.id.to\_s]  
if atts  
kid.attributes = atts  
kid.save  
else  
children.delete(kid)  
end  
end  
end  
end

And likewise for a new\_child\_attributes= method.

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.

Thanks!

-Roy

Roy P.  
Research Analyst/Programmer  
Group Health Center For Health Studies (Cancer Research Network)  
(206) 287-2078  
Google Talk: rpardee

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 15, 2009, 4:20am UTC](https://www.ruby-forum.com/t/helper-for-model/157761/2 "2009-01-15T04:20:49Z")

</div>

On Wed, Jan 14, 2009 at 8:26 PM, Pardee, Roy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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](http://training.collectiveidea.com) – San Antonio, TX – Jan 20-23

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 15, 2009, 6:18am UTC](https://www.ruby-forum.com/t/helper-for-model/157761/3 "2009-01-15T06:18:15Z")

</div>

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](http://iammrjoshua.com)

Brandon K. wrote:

> On Wed, Jan 14, 2009 at 8:26 PM, Pardee, Roy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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](http://training.collectiveidea.com) ï¿½ San Antonio, TX ï¿½ Jan 20-23

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 15, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/helper-for-model/157761/4 "2009-01-15T18:32:24Z")

</div>

Another option is to inject an abstract class model between AR and the  
rest of your models.

“some class” \< GenericModel \< ActiveRecord

where GenericModel implements all manner of standard infrastructure  
functionality that is written once. Very easy for a specific model to  
override default behaviors if necessary.

Did the same thing with my controllers as well.

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 16, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/helper-for-model/157761/5 "2009-01-16T17:38:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 16, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/helper-for-model/157761/6 "2009-01-16T18:15:45Z")

</div>

Yes. In your module you’ve defined an instance method not a class  
method.

But this:

> class CarePhase \< ActiveRecord::Base  
> include ModelHelper
> 
> ```
> pretend_helper("hi!")
> 
> ```

Is the class trying to call the method, which fails because the class  
doesn’t know about the ‘pretend\_helper’ method.

What you have would work fine if you did this:

CarePhase.new.pretend\_helper(‘hi’)

If you really do want a class method, try changing the method definition  
in PretendHelper to:

def self.pretend\_helper(msg)  
puts msg  
end

And then you would be fine to call:

CarePhase.pretend\_helper(‘hi’)

Hope that helps.

– Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Roy P. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [January 18, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/helper-for-model/157761/7 "2009-01-18T20:21:59Z")

</div>

Ah, of course–thanks! I do want an instance method I think–want to  
call the generics from instance methods in the class. I will  
experiment.

Thanks!

-Roy
