Is it possible to override the to_sentence
method just for one model in my rails application?
More generally, how do I modify methods for an Array of my models?
Is it possible to override the to_sentence
method just for one model in my rails application?
More generally, how do I modify methods for an Array of my models?
You can just defined *to_sentence* in that particular model.
class Model < ActiveRecord::Base
def to_sentence
end
end
When you are manipulating the results of a query in AR, each element of the array is an instance of your model class, so will automatically have all the methods and properties. The array itself will have all of the methods of the built-in Array and Enumerable classes (plus a few extras from AR). You can override or extend theses classes as well.
As you can imagine I'm quite the noob both on Rails and Stackoverflow, but I'm trying to get it right :)
I've simply created a helper on applcation_helper.rb
, that I can call in views whenever I need. Do you think that's the way to go, or is there a better way than that?
Anyways, I'm still curious to know if there's a way to override Array methods for Array containing a specific class/model :)