helper for model?

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 Pardee Research Analyst/Programmer Group Health Center For Health Studies (Cancer Research Network) (206) 287-2078 Google Talk: rpardee

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

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.

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

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