rails macro如何定义?where to define macros in Rails?
访问量: 3503
http://stackoverflow.com/questions/926489/rails-macro-style-functions
如何定义rails macro? 上面的文章描述的很全面了。 对于controller使用的,就定义在application controller中。对于model会使用的,就reopen activerecord::base
For models, you want to reopen ActiveRecord::Base:
module ActiveRecord class Base def self.acts_as_super_awesome do_more_awesome_stuff end end end
I personally would put that in a file in config/initializers so that it gets loaded once, and so that I know where to look for it always.
Then you can access it in models like so:
class MySuperAwesomeModel < ActiveRecord::Base acts_as_super_awesome end