views:

30

answers:

1

I have an after_find callback in a model, but I need to disable it in a particular controller action e.g.

def index
  @people = People.find(:all) # do something here to disable after_find()?
end

def show
  @people = People.find(:all) # after_find() should still be called here!
end

What is the best way to do it?

  1. Can I pass something in to .find to disable all/particular callbacks?
  2. Can I somehow get the controller name in the model and not execute the callback based on the controller name (I don't like this)..?

Help!

A: 

You can add a flag on your model to define if you want execute or not after_find.

class People
  @@callback_after_find = true
  def after_find
    return unless @@callback_after_find
    ...
  end
end

In your controller you can now activate or not this callback

def index
  People.callback_after_find = false
  @people = People.find(:all) # do something here to disable after_find()?
end

def show
  @people = People.find(:all) # after_find() should still be called here!
end
shingara