Skip to content
  • Markus Doits's avatar
    36685b19
    allow to autosave associations with polymorphic has many through join model and inverse of · 36685b19
    Markus Doits authored
    Before this was not possible:
    
    ```ruby
    class Zine < ActiveRecord::Base
      has_many :interests, inverse_of: :zine
      has_many :humans, through: :interests, source: :interestable, source_type: 'Human'
    end
    
    class Interest < ActiveRecord::Base
      belongs_to :zine, inverse_of: :interests
      belongs_to :interestable, polymorphic: true, inverse_of: :interests
    end
    
    class Human < ActiveRecord::Base
      has_many :interests, as: :interestable, inverse_of: :interestable
    end
    
    zine = Zine.create!
    zine.humans.create!
    
    # => ArgumentError: Polymorphic associations do not support computing the class.
    ```
    
    Now the code checks whether the association is polymorphic and uses the
    correct code to get the inverse association.
    36685b19
    allow to autosave associations with polymorphic has many through join model and inverse of
    Markus Doits authored
    Before this was not possible:
    
    ```ruby
    class Zine < ActiveRecord::Base
      has_many :interests, inverse_of: :zine
      has_many :humans, through: :interests, source: :interestable, source_type: 'Human'
    end
    
    class Interest < ActiveRecord::Base
      belongs_to :zine, inverse_of: :interests
      belongs_to :interestable, polymorphic: true, inverse_of: :interests
    end
    
    class Human < ActiveRecord::Base
      has_many :interests, as: :interestable, inverse_of: :interestable
    end
    
    zine = Zine.create!
    zine.humans.create!
    
    # => ArgumentError: Polymorphic associations do not support computing the class.
    ```
    
    Now the code checks whether the association is polymorphic and uses the
    correct code to get the inverse association.
Loading