例如:
module ModelHelper
def self.special_function(some_parameter)
do_some_special_thing
end
end
class Student < ActiveRecord::Base
def to_special
ModelHelper.special_function(a_variable_of_here)
end
end
class Teacher < ActiveRecord::Base
def to_special
ModelHelper.special_function(another_variable_of_here)
end
end
我把model_helper.rb
放在哪里?
我通常在lib中创建一个文件并包含它。比如lib/special_model.rb:
module SpecialModel
included do
def to_special
do_some_special_thing
end
end
end
然后在app/models/student.rb:
class Student
include SpecialModel
end
你可能还想看看activessupport::关注一些rails在使用模块时的帮助:
http://api.rubyonrails.org/classes/ActiveSupport/Concern.html