aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-09-12 23:32:50 +0100
committerJon Leighton <j@jonathanleighton.com>2012-09-12 23:32:50 +0100
commiteb4a623d74fe501ac73dc53d77cfca0d9d4ee01b (patch)
tree64e95beb96dc293aa3b5f277b05de58453f9e7cd /activerecord/test/cases
parentb5aed34c442550c4caad1fdca12c921d5ab9840f (diff)
downloadrails-eb4a623d74fe501ac73dc53d77cfca0d9d4ee01b.tar.gz
rails-eb4a623d74fe501ac73dc53d77cfca0d9d4ee01b.tar.bz2
rails-eb4a623d74fe501ac73dc53d77cfca0d9d4ee01b.zip
Fix nested association references
Previously the reflection would be looked up on the wrong class. However the test passed because the examples referred back to themselves.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/relation/where_test.rb69
1 files changed, 31 insertions, 38 deletions
diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb
index 76339fe41b..3163cf79ad 100644
--- a/activerecord/test/cases/relation/where_test.rb
+++ b/activerecord/test/cases/relation/where_test.rb
@@ -3,74 +3,67 @@ require 'models/author'
require 'models/price_estimate'
require 'models/treasure'
require 'models/post'
+require 'models/comment'
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
- fixtures :posts, :authors
+ fixtures :posts
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)
+ author = Author.new
+ author.id = 1
- assert_equal query_with_id.to_sql, query_with_assoc.to_sql
+ assert_equal Post.where(author_id: 1).to_sql, Post.where(author: author).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)
+ parent = Comment.new
+ parent.id = 1
- assert_equal query_with_id.to_sql, query_with_assoc.to_sql
+ expected = Post.where(comments: { parent_id: 1 }).joins(:comments)
+ actual = Post.where(comments: { parent: parent }).joins(:comments)
+
+ assert_equal expected.to_sql, actual.to_sql
end
def test_polymorphic_shallow_where
- treasure = Treasure.create(:name => 'gold coins')
- treasure.price_estimates << PriceEstimate.create(:price => 125)
+ treasure = Treasure.new
+ treasure.id = 1
- query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
- query_by_model = PriceEstimate.where(:estimate_of => treasure)
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: treasure)
- assert_equal query_by_column.to_sql, query_by_model.to_sql
+ assert_equal expected.to_sql, actual.to_sql
end
def test_polymorphic_sti_shallow_where
- treasure = HiddenTreasure.create!(:name => 'gold coins')
- treasure.price_estimates << PriceEstimate.create!(:price => 125)
+ treasure = HiddenTreasure.new
+ treasure.id = 1
- query_by_column = PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
- query_by_model = PriceEstimate.where(:estimate_of => treasure)
+ expected = PriceEstimate.where(estimate_of_type: 'Treasure', estimate_of_id: 1)
+ actual = PriceEstimate.where(estimate_of: treasure)
- assert_equal query_by_column.to_sql, query_by_model.to_sql
+ assert_equal expected.to_sql, actual.to_sql
end
def test_polymorphic_nested_where
- estimate = PriceEstimate.create :price => 125
- treasure = Treasure.create :name => 'Booty'
-
- treasure.price_estimates << estimate
+ thing = Post.new
+ thing.id = 1
- 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)
+ expected = Treasure.where(price_estimates: { thing_type: 'Post', thing_id: 1 }).joins(:price_estimates)
+ actual = Treasure.where(price_estimates: { thing: thing }).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
+ assert_equal expected.to_sql, actual.to_sql
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
+ treasure = HiddenTreasure.new
+ treasure.id = 1
- 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)
+ expected = Treasure.where(price_estimates: { estimate_of_type: 'Treasure', estimate_of_id: 1 }).joins(:price_estimates)
+ actual = 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
+ assert_equal expected.to_sql, actual.to_sql
end
def test_where_error