In Rails 2.3 I have a Rails engine which has its own namespaced controllers, but which will use the main app’s models.
However, I want to override the default behavior of ActiveRecord::Base#to_json method but only when the model is being used from the Engine.
So in an Engine controller is it reasonable to do something like…
category.questions.each do |qst|
# override ActiveRecord::Base#to_json somehow on the qst instance
end
…if it is reasonable conceptually, how would I actually go about such a thing?
If it’s not reasonable, what is the best way to override ActiveRecord::Base#to_json for the Engine only, and not the main app?
Thanks,
Paul
You can do this using
instance_eval:However, I’m not sure if this is actually an appropriate strategy for what you want to do. A simpler approach would be to make a differently named method and use that in place of
to_jsonin your engine.