Ruby on rails 在RubyonRails中将模块混合到模型中
我希望将多个模型共享的功能抽象为一个模块 我有这个模型:Ruby on rails 在RubyonRails中将模块混合到模型中,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我希望将多个模型共享的功能抽象为一个模块 我有这个模型: class Report < ActiveRecord::Base include AppModel has_and_belongs_to_many :tag belongs_to :city belongs_to :user end 但是如果我做了Report.list它就不起作用了。如果在模块中我执行def功能或def自我功能,我怀疑有问题 因此,我的问题是,如何使诸如Report.list或Report.fi
class Report < ActiveRecord::Base
include AppModel
has_and_belongs_to_many :tag
belongs_to :city
belongs_to :user
end
但是如果我做了Report.list
它就不起作用了。如果在模块中我执行def功能
或def自我功能
,我怀疑有问题
因此,我的问题是,如何使诸如
Report.list
或Report.find\u taged
之类的函数可以访问,这些函数是在模块中定义的。包括混合实例方法,而不是类级别。使用extend方法或verrideinclude
注意:要在上使用代码格式,请使用{}包含混合实例方法,而不是类级别。使用extend方法或verrideinclude 另外,要在上使用代码格式,请使用{}您可以由John Nunemaker阅读您可以由John Nunemaker阅读
module AppModel
def self.list
find(:all, :order => 'name asc').map { |item| [item.name, item.id] }
end
end