Ticket #10878: has_many_through_non_conventional_ids_fix.patch
| File has_many_through_non_conventional_ids_fix.patch, 5.5 kB (added by codafoo, 9 months ago) |
|---|
-
test/schema/schema.rb
old new 355 355 create_table 'warehouse-things', :force => true do |t| 356 356 t.integer :value 357 357 end 358 359 create_table :owners, :primary_key => :owner_id ,:force => true do |t| 360 t.string :name 361 end 362 363 create_table :pets, :primary_key => :pet_id ,:force => true do |t| 364 t.string :name 365 t.integer :owner_id, :integer 366 end 367 368 create_table :pet_accessories, :primary_key => :pet_accessory_id, :force => true do |t| 369 t.string :name 370 t.integer :pet_id, :integer 371 end 358 372 end -
test/models/pet.rb
old new 1 class Pet < ActiveRecord::Base 2 set_primary_key :pet_id 3 belongs_to :owner 4 has_many :pet_accessories 5 end -
test/models/owner.rb
old new 1 class Owner < ActiveRecord::Base 2 set_primary_key :owner_id 3 has_many :pets 4 has_many :pet_accessories, :through => :pets 5 end -
test/models/pet_accessory.rb
old new 1 class PetAccessory < ActiveRecord::Base 2 set_primary_key :pet_accessory_id 3 belongs_to :pet 4 end -
test/fixtures/pet_accessories.yml
old new 1 cracker: 2 pet_accessory_id: 1 3 pet_id: 1 4 name: cracker 5 6 eyepatch: 7 pet_accessory_id: 2 8 pet_id: 1 9 name: eyepatch 10 11 leash: 12 pet_accessory_id: 3 13 pet_id: 2 14 name: leash 15 16 collar: 17 pet_accessory_id: 4 18 pet_id: 3 19 name: collar 20 21 harness: 22 pet_accessory_id: 5 23 pet_id: 3 24 name: harness -
test/fixtures/pets.yml
old new 1 parrot: 2 pet_id: 1 3 name: parrot 4 owner_id: 1 5 6 chew: 7 pet_id: 2 8 name: chew 9 owner_id: 2 10 11 mochi: 12 pet_id: 3 13 name: mochi 14 owner_id: 2 -
test/fixtures/owners.yml
old new 1 blackbeard: 2 owner_id: 1 3 name: blackbeard 4 5 ashley: 6 owner_id: 2 7 name: ashley -
test/cases/associations/join_model_test.rb
old new 11 11 require 'models/edge' 12 12 require 'models/book' 13 13 require 'models/citation' 14 require 'models/owner' 15 require 'models/pet' 16 require 'models/pet_accessory' 14 17 15 18 class AssociationsJoinModelTest < ActiveSupport::TestCase 16 19 self.use_transactional_fixtures = false 17 fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books 20 fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books, :owners, :pets, :pet_accessories 18 21 19 22 def test_has_many 20 23 assert authors(:david).categories.include?(categories(:general)) … … 233 236 assert_equal posts[i].authors.length, assert_no_queries { posts_with_authors[i].authors.length } 234 237 end 235 238 end 239 240 def test_include_has_many_through_non_conventional_primary_key 241 accessories = PetAccessory.find(:all, :order => 'pet_accessory_id', :conditions => {:pet_id => 1}) 242 owner = Pet.find(1).owner 243 owner_accessories = owner.pet_accessories.find(:all,:order => 'pet_accessory_id') 244 assert_equal owner_accessories.length, accessories.length 245 end 236 246 237 247 def test_include_polymorphic_has_one 238 248 post = Post.find_by_id(posts(:welcome).id, :include => :tagging) -
lib/active_record/associations/has_many_through_association.rb
old new 214 214 end 215 215 else 216 216 reflection_primary_key = @reflection.source_reflection.primary_key_name 217 source_primary_key = @reflection. klass.primary_key217 source_primary_key = @reflection.through_reflection.klass.primary_key 218 218 if @reflection.source_reflection.options[:as] 219 219 polymorphic_join = "AND %s.%s = %s" % [ 220 220 @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type", … … 222 222 ] 223 223 end 224 224 end 225 226 225 "INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [ 227 226 @reflection.through_reflection.table_name, 228 227 @reflection.table_name, reflection_primary_key,