我可以在 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 类具有不同的类层次结构?
最佳答案
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
关于ruby-on-rails - 将模块与 attr_accessible、has_one、has_many 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913922/