From 3fa61ccb9eed0f17cdef85470ae708b4b09a3c06 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 26 Jan 2011 23:00:13 +0000 Subject: [PATCH] Has many through - It is not necessary to manually merge in the conditions hash for the through record, because the creation is done directly on the through association, which will already handle setting the conditions. --- .../associations/through_association.rb | 4 ---- .../has_many_through_associations_test.rb | 12 +++++++++++- activerecord/test/models/club.rb | 3 ++- activerecord/test/models/member.rb | 3 +++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index ac62f854e8..be1fc79846 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -88,10 +88,6 @@ module ActiveRecord join_attributes.merge!(@reflection.source_reflection.foreign_type => associate.class.base_class.name) end - if @reflection.through_reflection.options[:conditions].is_a?(Hash) - join_attributes.merge!(@reflection.through_reflection.options[:conditions]) - end - join_attributes end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 7235631b5a..96f4597726 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -20,10 +20,13 @@ require 'models/subscription' require 'models/categorization' require 'models/category' require 'models/essay' +require 'models/member' +require 'models/membership' +require 'models/club' class HasManyThroughAssociationsTest < ActiveRecord::TestCase fixtures :posts, :readers, :people, :comments, :authors, :categories, - :owners, :pets, :toys, :jobs, :references, :companies, + :owners, :pets, :toys, :jobs, :references, :companies, :members, :subscribers, :books, :subscriptions, :developers, :categorizations # Dummies to force column loads so query counts are clean. @@ -557,4 +560,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert proxy.stale_target? assert_equal authors(:david).categorizations.sort_by(&:id), post.author_categorizations.sort_by(&:id) end + + def test_create_with_conditions_hash_on_through_association + member = members(:groucho) + club = member.clubs.create! + + assert_equal true, club.reload.membership.favourite + end end diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb index 6e7cdd643a..c432a6ace8 100644 --- a/activerecord/test/models/club.rb +++ b/activerecord/test/models/club.rb @@ -1,4 +1,5 @@ class Club < ActiveRecord::Base + has_one :membership has_many :memberships has_many :members, :through => :memberships has_many :current_memberships @@ -10,4 +11,4 @@ class Club < ActiveRecord::Base def private_method "I'm sorry sir, this is a *private* club, not a *pirate* club" end -end \ No newline at end of file +end diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 15ad6aedd3..e6e78f9e45 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -13,4 +13,7 @@ class Member < ActiveRecord::Base has_many :current_memberships has_one :club_through_many, :through => :current_memberships, :source => :club + + has_many :current_memberships, :conditions => { :favourite => true } + has_many :clubs, :through => :current_memberships end