Topic: before_save callback

So after getting my head around regex, I finally have a string method I would like to use each time a document is created or updated. I have this method sitting inside of a String Helper:

class String
  def make_permalink
    downcase.sub(/^\./, 'dot-').gsub(/&/, '-and-').gsub(/\W|\s+/, '-').gsub(/[^a-zA-Z0-9_-]+/, '')
  def update_links   
    gsub(/:\/([a-z])\/+/, ':/terms/\1/').gsub(/(:\/1\/)+/, ':/terms/numbers/')                                                         

For the record, that permalink method is working perfectly, so I know this class is loading correctly. The problem is with the update_links method. Strangely enough I get it working just fine from the console like so:

>>  t = Term.find_by_permalink('test')
>> t.body.update_links

The above gives me the desired result everytime, so I decide to use a callback to make sure the links are saved properly.
class Term < ActiveRecord::Base
   before_save :body_update_links
   def body_update_links

I save, restart Mongrel, but no matter what I do, niether saving nor updating my term makes any difference.

Am I going about this all wrong?

Re: before_save callback

Man I am on a roll with helping myself today!

def before_save
   self.body = body.update_links

I think I am getting smarter...or at least less clueless

Last edited by pimpmaster (2007-04-11 12:13:54)