Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Respect the :primary_key option on the through_reflection of (non-nes…

…ted) through associations
  • Loading branch information...
commit 596cc3b2329a9cc4a30c95c157ce36b2d08975df 1 parent 9ec0734
@jonleighton jonleighton authored
View
6 activerecord/lib/active_record/associations/has_many_association.rb
@@ -7,9 +7,9 @@ module Associations
# is provided by its child HasManyThroughAssociation.
class HasManyAssociation < AssociationCollection #:nodoc:
protected
- def owner_quoted_id
- if @reflection.options[:primary_key]
- @owner.class.quote_value(@owner.send(@reflection.options[:primary_key]))
+ def owner_quoted_id(reflection = @reflection)
+ if reflection.options[:primary_key]
+ @owner.class.quote_value(@owner.send(reflection.options[:primary_key]))
else
@owner.quoted_id
end
View
6 activerecord/lib/active_record/associations/has_one_association.rb
@@ -64,9 +64,9 @@ def replace(obj, dont_save = false)
end
protected
- def owner_quoted_id
- if @reflection.options[:primary_key]
- @owner.class.quote_value(@owner.send(@reflection.options[:primary_key]))
+ def owner_quoted_id(reflection = @reflection)
+ if reflection.options[:primary_key]
+ @owner.class.quote_value(@owner.send(reflection.options[:primary_key]))
else
@owner.quoted_id
end
View
4 activerecord/lib/active_record/associations/through_association_scope.rb
@@ -44,14 +44,14 @@ def construct_conditions
# Associate attributes pointing to owner, quoted.
def construct_quoted_owner_attributes(reflection)
if as = reflection.options[:as]
- { "#{as}_id" => owner_quoted_id,
+ { "#{as}_id" => owner_quoted_id(reflection),
"#{as}_type" => reflection.klass.quote_value(
@owner.class.base_class.name.to_s,
reflection.klass.columns_hash["#{as}_type"]) }
elsif reflection.macro == :belongs_to
{ reflection.klass.primary_key => @owner.class.quote_value(@owner[reflection.primary_key_name]) }
else
- { reflection.primary_key_name => owner_quoted_id }
+ { reflection.primary_key_name => owner_quoted_id(reflection) }
end
end
View
19 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -17,11 +17,14 @@
require 'models/subscriber'
require 'models/book'
require 'models/subscription'
+require 'models/essay'
+require 'models/category'
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors,
:owners, :pets, :toys, :jobs, :references, :companies,
- :subscribers, :books, :subscriptions, :developers
+ :subscribers, :books, :subscriptions, :developers,
+ :essays, :categories
# Dummies to force column loads so query counts are clean.
def setup
@@ -449,4 +452,18 @@ def test_include_method_in_association_through_should_return_true_for_instance_a
comment = post.comments.build
assert author.comments.include?(comment)
end
+
+ def test_has_many_through_polymorphic_with_primary_key_option_on_through_reflection
+ assert_equal [categories(:general)], authors(:david).essay_categories
+
+ authors = Author.joins(:essay_categories).where('categories.id' => categories(:general).id)
+ assert_equal authors(:david), authors.first
+ end
+
+ def test_has_many_through_with_primary_key_option_on_through_reflection
+ assert_equal [categories(:general)], authors(:david).essay_categories_2
+
+ authors = Author.joins(:essay_categories_2).where('categories.id' => categories(:general).id)
+ assert_equal authors(:david), authors.first
+ end
end
View
20 activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -9,9 +9,13 @@
require 'models/minivan'
require 'models/dashboard'
require 'models/speedometer'
+require 'models/category'
+require 'models/author'
+require 'models/essay'
class HasOneThroughAssociationsTest < ActiveRecord::TestCase
- fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans, :dashboards, :speedometers
+ fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
+ :dashboards, :speedometers, :categories, :authors, :essays
def setup
@member = members(:groucho)
@@ -212,4 +216,18 @@ def test_value_is_properly_quoted
minivan.dashboard
end
end
+
+ def test_has_one_through_polymorphic_with_primary_key_option_on_through_reflection
+ assert_equal categories(:general), authors(:david).essay_category
+
+ authors = Author.joins(:essay_category).where('categories.id' => categories(:general).id)
+ assert_equal authors(:david), authors.first
+ end
+
+ def test_has_one_through_with_primary_key_option_on_through_reflection
+ assert_equal categories(:general), authors(:david).essay_category_2
+
+ authors = Author.joins(:essay_category_2).where('categories.id' => categories(:general).id)
+ assert_equal authors(:david), authors.first
+ end
end
View
6 activerecord/test/fixtures/essays.yml
@@ -0,0 +1,6 @@
+david_modest_proposal:
+ name: A Modest Proposal
+ writer_type: Author
+ writer_id: David
+ category_id: 1
+ author_id: David
View
12 activerecord/test/models/author.rb
@@ -95,8 +95,18 @@ def testing_proxy_target
has_many :subscriptions, :through => :books
has_many :subscribers, :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection)
has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick"
-
+
has_one :essay, :primary_key => :name, :as => :writer
+ has_one :essay_category, :through => :essay, :source => :category
+
+ has_one :essay_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
+ has_one :essay_category_2, :through => :essay_2, :source => :category
+
+ has_many :essays, :primary_key => :name, :as => :writer
+ has_many :essay_categories, :through => :essays, :source => :category
+
+ has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
+ has_many :essay_categories_2, :through => :essays_2, :source => :category
belongs_to :author_address, :dependent => :destroy
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
View
1  activerecord/test/models/essay.rb
@@ -1,3 +1,4 @@
class Essay < ActiveRecord::Base
belongs_to :writer, :primary_key => :name, :polymorphic => true
+ belongs_to :category
end
View
2  activerecord/test/schema/schema.rb
@@ -214,6 +214,8 @@ def create_table(*args, &block)
t.string :name
t.string :writer_id
t.string :writer_type
+ t.integer :category_id
+ t.integer :author_id
end
create_table :events, :force => true do |t|
Please sign in to comment.
Something went wrong with that request. Please try again.