gpt4 book ai didi

ruby-on-rails - 将模块与 attr_accessible、has_one、has_many 混合

转载 作者:行者123 更新时间:2023-12-04 01:38:19 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com