Skip to content
Permalink
Browse files

Fixed several bugs in has_one and has_many associations when :primary…

…_key specified
  • Loading branch information
kpumuk committed Dec 16, 2009
1 parent 050d832 commit 3e29891cf521186290570e3e489de079b085e193
Showing with 33 additions and 4 deletions.
  1. +4 −3 lib/composite_primary_keys/associations.rb
  2. +3 −0 test/fixtures/membership.rb
  3. +26 −1 test/test_associations.rb
@@ -180,11 +180,12 @@ def composite_association_join
raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
else
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
primary_key = options[:primary_key] || parent.primary_key
" LEFT OUTER JOIN %s ON %s " % [
table_name_and_alias,
composite_join_clause(
full_keys(aliased_table_name, foreign_key),
full_keys(parent.aliased_table_name, parent.primary_key)),
full_keys(parent.aliased_table_name, primary_key)),
]
end
when :belongs_to
@@ -338,7 +339,7 @@ def construct_sql
@finder_sql << " AND (#{conditions})" if conditions

else
@finder_sql = full_columns_equals(@reflection.klass.table_name, @reflection.primary_key_name, @owner.quoted_id)
@finder_sql = full_columns_equals(@reflection.klass.table_name, @reflection.primary_key_name, owner_quoted_id)
@finder_sql << " AND (#{conditions})" if conditions
end

@@ -386,7 +387,7 @@ def construct_sql
"#{@reflection.klass.quoted_table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
"#{@reflection.klass.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
else
@finder_sql = full_columns_equals(@reflection.klass.table_name, @reflection.primary_key_name, @owner.quoted_id)
@finder_sql = full_columns_equals(@reflection.klass.table_name, @reflection.primary_key_name, owner_quoted_id)
end

@finder_sql << " AND (#{conditions})" if conditions
@@ -4,4 +4,7 @@ class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]

has_many :readings, :primary_key => :user_id, :foreign_key => :user_id
has_one :reading, :primary_key => :user_id, :foreign_key => :user_id, :order => 'id DESC'
end
@@ -17,7 +17,8 @@

class TestAssociations < ActiveSupport::TestCase
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings,
:memberships

def test_has_many_through_with_conditions_when_through_association_is_not_composite
user = User.find(:first)
@@ -157,4 +158,28 @@ def test_has_and_belongs_to_many
@restaurant = Restaurant.find([1,1], :include => :suburbs)
assert_equal 2, @restaurant.suburbs.size
end

def test_has_many_with_primary_key
@membership = Membership.find([1, 1])

assert_equal 2, @membership.readings.size
end

def test_has_one_with_primary_key
@membership = Membership.find([1, 1])

assert_equal 2, @membership.reading.id
end

def test_joins_has_many_with_primary_key
@membership = Membership.find(:first, :joins => :readings, :conditions => { :readings => { :id => 1 } })

assert_equal [1, 1], @membership.id
end

def test_joins_has_one_with_primary_key
@membership = Membership.find(:first, :joins => :reading, :conditions => { :readings => { :id => 2 } })

assert_equal [1, 1], @membership.id
end
end

0 comments on commit 3e29891

Please sign in to comment.
You can’t perform that action at this time.