11

我可以在 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

或者我是否需要使用简单的继承,即使我的 Authored 类具有不同的类层次结构?

4

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

有关更多信息ActiveSupport::Concern,请访问http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

于 2012-10-16T11:47:45.870 回答