Skip to content

Commit

Permalink
Respect the :primary_key option on the through_reflection of (non-nes…
Browse files Browse the repository at this point in the history
…ted) through associations
  • Loading branch information
jonleighton committed Oct 19, 2010
1 parent 9ec0734 commit 596cc3b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 11 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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

Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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)
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions 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
12 changes: 11 additions & 1 deletion activerecord/test/models/author.rb
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions 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
2 changes: 2 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -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|
Expand Down

0 comments on commit 596cc3b

Please sign in to comment.