Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8776

Show
Ignore:
Timestamp:
02/02/08 03:27:31 (7 months ago)
Author:
bitsweat
Message:

Fix has_many :through a polymorphic has_many. Closes #10529 [Aleksey Kondratenko]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r8742 r8776  
    213213        def construct_joins(custom_joins = nil) 
    214214          polymorphic_join = nil 
    215           if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to 
     215          if @reflection.source_reflection.macro == :belongs_to 
    216216            reflection_primary_key = @reflection.klass.primary_key 
    217217            source_primary_key     = @reflection.source_reflection.primary_key_name 
  • trunk/activerecord/test/cases/associations_test.rb

    r8735 r8776  
    1717require 'models/person' 
    1818require 'models/reader' 
     19require 'models/parrot' 
     20require 'models/pirate' 
     21require 'models/treasure' 
     22require 'models/price_estimate' 
    1923 
    2024class AssociationsTest < ActiveRecord::TestCase 
     
    16081612 
    16091613class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase 
    1610   fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects 
     1614  fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects, 
     1615           :parrots, :pirates, :treasures, :price_estimates 
    16111616 
    16121617  def test_has_and_belongs_to_many 
     
    21442149    end 
    21452150  end 
     2151 
     2152  def test_has_many_through_polymorphic_has_manys_works 
     2153    assert_equal [10, 20].to_set, pirates(:redbeard).treasure_estimates.map(&:price).to_set 
     2154  end 
    21462155end 
    21472156 
  • trunk/activerecord/test/models/pirate.rb

    r8657 r8776  
    22  belongs_to :parrot 
    33  has_and_belongs_to_many :parrots 
    4   has_many :loots, :as => :looter 
     4  has_many :treasures, :as => :looter 
     5 
     6  has_many :treasure_estimates, :through => :treasures, :source => :price_estimates 
    57end 
  • trunk/activerecord/test/models/treasure.rb

    r8657 r8776  
    22  has_and_belongs_to_many :parrots 
    33  belongs_to :looter, :polymorphic => true 
     4 
     5  has_many :price_estimates, :as => :estimate_of 
    46end 
  • trunk/activerecord/test/schema/schema.rb

    r8762 r8776  
    368368    t.integer :owner_id, :integer 
    369369  end 
     370 
     371  create_table :price_estimates, :force => true do |t| 
     372    t.string :estimate_of_type 
     373    t.integer :estimate_of_id 
     374    t.integer :price 
     375  end 
    370376end