lkahtz
lkahtz

Reputation: 4796

Writing a new instance method for a common class in rails 3 globally accessible in the whole app

In which file could I add a new method to a common class, like String, Array etc, so that all models can reuse the method?

For example, in user.rb I can define:

class String
  def strip_non_word_chars
    self.gsub!(/(\W)/," ")
  end
end

class User < ActiveRecord::Base
end

But other models cannot use the new strip_non_word_chars method. I want it to be useable by all models...

Upvotes: 0

Views: 311

Answers (1)

raid5ive
raid5ive

Reputation: 6642

I believe one place would be in an initializer file in the config/initializers directory. Although I'm not sure if this is common for your task.

Upvotes: 2

Related Questions