diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 14:04:11 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 14:04:11 -0700 |
commit | b5aed34c442550c4caad1fdca12c921d5ab9840f (patch) | |
tree | e19d1e7147070f25fcd18754211913552f99dde1 /activerecord/test | |
parent | 0f228b420e26584e26702935135a4b099df85696 (diff) | |
parent | 3da275c4396d7fad250d2b786027ba4f14344bd4 (diff) | |
download | rails-b5aed34c442550c4caad1fdca12c921d5ab9840f.tar.gz rails-b5aed34c442550c4caad1fdca12c921d5ab9840f.tar.bz2 rails-b5aed34c442550c4caad1fdca12c921d5ab9840f.zip |
Merge pull request #7273 from beerlington/foreign_key_model_queries
Convert model name to foreign key in queries
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/relation/where_test.rb | 70 | ||||
-rw-r--r-- | activerecord/test/models/treasure.rb | 3 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
3 files changed, 73 insertions, 1 deletions
diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index 90c690e266..76339fe41b 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -1,9 +1,77 @@ require "cases/helper" +require 'models/author' +require 'models/price_estimate' +require 'models/treasure' require 'models/post' module ActiveRecord class WhereTest < ActiveRecord::TestCase - fixtures :posts + fixtures :posts, :authors + + def test_belongs_to_shallow_where + author = Post.first.author + query_with_id = Post.where(:author_id => author) + query_with_assoc = Post.where(:author => author) + + assert_equal query_with_id.to_sql, query_with_assoc.to_sql + end + + def test_belongs_to_nested_where + author = Post.first.author + query_with_id = Author.where(:posts => {:author_id => author}).joins(:posts) + query_with_assoc = Author.where(:posts => {:author => author}).joins(:posts) + + assert_equal query_with_id.to_sql, query_with_assoc.to_sql + end + + def test_polymorphic_shallow_where + treasure = Treasure.create(:name => 'gold coins') + treasure.price_estimates << PriceEstimate.create(:price => 125) + + query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) + query_by_model = PriceEstimate.where(:estimate_of => treasure) + + assert_equal query_by_column.to_sql, query_by_model.to_sql + end + + def test_polymorphic_sti_shallow_where + treasure = HiddenTreasure.create!(:name => 'gold coins') + treasure.price_estimates << PriceEstimate.create!(:price => 125) + + query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) + query_by_model = PriceEstimate.where(:estimate_of => treasure) + + assert_equal query_by_column.to_sql, query_by_model.to_sql + end + + def test_polymorphic_nested_where + estimate = PriceEstimate.create :price => 125 + treasure = Treasure.create :name => 'Booty' + + treasure.price_estimates << estimate + + query_by_column = Treasure.where(:price_estimates => {:estimate_of_type => 'Treasure', :estimate_of_id => treasure}).joins(:price_estimates) + query_by_model = Treasure.where(:price_estimates => {:estimate_of => treasure}).joins(:price_estimates) + + assert_equal treasure, query_by_column.first + assert_equal treasure, query_by_model.first + assert_equal query_by_column.to_a, query_by_model.to_a + end + + def test_polymorphic_sti_nested_where + estimate = PriceEstimate.create :price => 125 + treasure = HiddenTreasure.create!(:name => 'gold coins') + treasure.price_estimates << PriceEstimate.create!(:price => 125) + + treasure.price_estimates << estimate + + query_by_column = Treasure.where(:price_estimates => {:estimate_of_type => 'Treasure', :estimate_of_id => treasure}).joins(:price_estimates) + query_by_model = Treasure.where(:price_estimates => {:estimate_of => treasure}).joins(:price_estimates) + + assert_equal treasure, query_by_column.first + assert_equal treasure, query_by_model.first + assert_equal query_by_column.to_a, query_by_model.to_a + end def test_where_error assert_raises(ActiveRecord::StatementInvalid) do diff --git a/activerecord/test/models/treasure.rb b/activerecord/test/models/treasure.rb index 2a98e74f2c..e864295acf 100644 --- a/activerecord/test/models/treasure.rb +++ b/activerecord/test/models/treasure.rb @@ -6,3 +6,6 @@ class Treasure < ActiveRecord::Base accepts_nested_attributes_for :looter end + +class HiddenTreasure < Treasure +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index b4e611cb09..798ea20efc 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -693,6 +693,7 @@ ActiveRecord::Schema.define do create_table :treasures, :force => true do |t| t.column :name, :string + t.column :type, :string t.column :looter_id, :integer t.column :looter_type, :string end |