Can I do somehting like this in Rails ?
我可以在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 ?
或者我是否需要使用简单继承,即使我的Authored类具有不同的类层次结构?
1 个解决方案
#1
23
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
有关ActiveSupport :: Concern的更多信息,请访问http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
#1
23
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
有关ActiveSupport :: Concern的更多信息,请访问http://api.rubyonrails.org/classes/ActiveSupport/Concern.html