I would like to have functionality shared by several models to be abstracted into a module.
I have this model:
class Report < ActiveRecord::Base
include AppModel
has_and_belongs_to_many :tag
belongs_to :city
belongs_to :user
end
(also, how do I make my code span multiple lines on stackoverflow? it doesn`t preserve my newlines.)
Then I have a module (in app/models/app_model.rb):
module AppModel
def self.list
find(:all, :order => 'name asc').map { |item| [item.name, item.id] }
end
end
but if I do Report.list it doesn’t work. I suspect there’s a problem if in a module I do def function or def self.function?
So again my question is, how do I make accessible a function such as Report.list or Report.find_tagged that is defined in a module.
include mix the instances methods, not class-level. Use extend method or orverride include.
P.S. to use code-formatting on SO use {}