Reliqs

a collection

Common behaviour for ActiveRecord models

A simple way to define a common behaviour for ActiveRecord models, avoiding to fall into model inheritance.

The method acts_as_* have to be called by models.

 
module CommonBehaviour   
  def acts_as_common     
    belongs_to :content       
    cattr_accessor :shared   
  end 
end 
ActiveRecord::Base.extend CommonBehaviour 

... 

require 'common_behaviour' 
class Article < ActiveRecord::Base   
  acts_as_common   
  attr_accessible :abstract, :body, :title 
end 

References

Published on 03/04/2013 at 09:28 under . Tags ,

Comment Common behaviour for ActiveRecord models

Powered by Typo – Thème Frédéric de Villamil | Photo Glenn