【问题标题】:rails 4 - associations - is it possible to have a polymorphic association on one side of a many-through?rails 4 - 关联 - 是否可以在多通的一侧有一个多态关联?
【发布时间】:2015-10-11 14:09:14
【问题描述】:

我有一个 Has-Many:Through 关联,其中一侧 (AccessLevel ) 是定义的模型 'AccessLevel' ,但另一侧可以设置多个资源

我定义了要包含在这些模型中的特定关注点,并尝试使用多态关联

    class AccessLevel < ActiveRecord::Base
      has_many :allowed_activities 
      has_many :actionable, through: :allowed_activities    
    end

    class AllowedActivity < ActiveRecord::Base
      belongs_to :access_level
      belongs_to :actionable, polymorphic: true
    end


    class <Model> < ActiveRecord::Base
      include Authorizable
    end

    module Authorizable
      extend ActiveSupport::Concern
      included do
        has_many :allowed_activities, as: :actionable
        has_many :access_levels, through: :allowed_activities
      end
    end

这是一个正确的实现还是我会提出一些意想不到的附带问题?

感谢反馈

【问题讨论】:

    标签: ruby-on-rails-4 activerecord polymorphic-associations


    【解决方案1】:

    不可能在多态对象上使用 has_many: :through

    我需要指定每个对象的 source_type ( Sheet, ...

      has_many :actionable_sheets, :through => :allowed_activities, :source => :actionable, :source_type => 'Sheet'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-05
      • 2016-03-20
      • 2011-01-30
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多