This project is archived and is in readonly mode.

#3520 ✓resolved
George Ogata

Add inverse polymorphic association support

Reported by George Ogata | November 29th, 2009 @ 08:43 PM | in 2.3.6

The attached patch adds support for :inverse_of on a polymorphic belongs_to association.

require 'active_record'
ActiveRecord::Base.establish_connection('adapter' => "sqlite3", 'database' => ":memory:")
ActiveRecord::Schema.define do
  create_table :parents do
  end
  create_table :children do |t|
    t.integer :parent_id
    t.string :parent_type
  end
end

class Parent < ActiveRecord::Base
  has_one :child, :as => :parent, :inverse_of => :parent
end

class Child < ActiveRecord::Base
  belongs_to :parent, :polymorphic => true, :inverse_of => :child
end

parent = Parent.new(:child => Child.new)
parent.child.parent.target.equal?(parent)  # true

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages