aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relation
diff options
context:
space:
mode:
authorbeerlington <pete@lette.us>2012-09-11 14:11:51 -0400
committerbeerlington <pete@lette.us>2012-09-11 14:11:51 -0400
commit3da275c4396d7fad250d2b786027ba4f14344bd4 (patch)
treea0367f680705af28ea053e890f82b359fa511a11 /activerecord/test/cases/relation
parentf2a44ade14f995d0574bedf79c8d1bee520f0306 (diff)
downloadrails-3da275c4396d7fad250d2b786027ba4f14344bd4.tar.gz
rails-3da275c4396d7fad250d2b786027ba4f14344bd4.tar.bz2
rails-3da275c4396d7fad250d2b786027ba4f14344bd4.zip
Accept belongs_to assoc. keys in ActiveRecord queries
Allows you to specify the model association key in a belongs_to relationship instead of the foreign key. The following queries are now equivalent: Post.where(:author_id => Author.first) Post.where(:author => Author.first) PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) PriceEstimate.where(:estimate_of => treasure)
Diffstat (limited to 'activerecord/test/cases/relation')
-rw-r--r--activerecord/test/cases/relation/where_test.rb70
1 files changed, 69 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