ruby-on-rails – Ruby on Rails – 如何委托多态关联?
发布时间:2020-12-17 03:15:09 所属栏目:百科 来源:网络整理
导读:是否可以在多态模型中使用has_many或has_one关联的委托?这是如何运作的? class Generic ActiveRecord::Base ... belongs_to :generable,polymorphic: true delegate :file_url,to: :image,allow_nil: true delegate :type_cat,to: :cat,allow_nil: trueend
是否可以在多态模型中使用has_many或has_one关联的委托?这是如何运作的?
class Generic < ActiveRecord::Base ... belongs_to :generable,polymorphic: true delegate :file_url,to: :image,allow_nil: true delegate :type_cat,to: :cat,allow_nil: true end class Image < ActiveRecord::Base ... has_one :generic,as: generable,dependent: :destroy end class Cat < ActiveRecord::Base ... has_one :generic,dependent: :destroy end 解决方法
不确定这是否与你想要做的完全匹配,因为从你的例子中很难说但是……
class Generic < ActiveRecord::Base ... belongs_to :generable,polymorphic: true ... delegate :common_method,to: :generable,prefix: true end class Cat def common_method ... end end class Image def common_method ... end end 允许您说出以下内容: generic.generable_common_method (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |