Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing in module with attr_accessible, has_one, has_many

Can I do somehting like this in Rails ?

module Authored
  belongs_to :user
  attr_accessible creation_date
end

class Line < ActiveRecord::Base
  include Authored
end

class Document < ActiveRecord::Base
  include Authored
end

class User < ActiveRecord::Base
  has_many :creations, :class_name => 'Authored'
end

Or do I need to use simple inheritance, even if my Authored classes have different class hierarchies ?

like image 960
ejoubaud Avatar asked Oct 16 '12 11:10

ejoubaud


1 Answers

module Authored
  extend ActiveSupport::Concern

  included do
    belongs_to :user
    attr_accessible :creation_date
  end
end

class Line < ActiveRecord::Base
  include Authored
end

class Document < ActiveRecord::Base
  include Authored
end

For more info on ActiveSupport::Concern, http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

like image 65
axsuul Avatar answered Oct 05 '22 07:10

axsuul