提交 81246994 编写于 作者: N Neeraj Singh

has_many through obeys order on through association

fixes #10016
上级 db113d90
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* `has_many` using `:through` now obeys the order clause mentioned in
through association. Fixes #10016.
*Neeraj Singh*
* `belongs_to :touch` behavior now touches old association when * `belongs_to :touch` behavior now touches old association when
transitioning to new association. transitioning to new association.
......
...@@ -101,6 +101,7 @@ def add_constraints(scope) ...@@ -101,6 +101,7 @@ def add_constraints(scope)
scope.includes! item.includes_values scope.includes! item.includes_values
scope.where_values += item.where_values scope.where_values += item.where_values
scope.order_values += (item.order_values - scope.order_values)
end end
end end
......
...@@ -302,7 +302,7 @@ def test_nested_loading_through_has_one_association_with_conditions_on_nested_as ...@@ -302,7 +302,7 @@ def test_nested_loading_through_has_one_association_with_conditions_on_nested_as
def test_eager_association_loading_with_belongs_to_and_foreign_keys def test_eager_association_loading_with_belongs_to_and_foreign_keys
pets = Pet.all.merge!(:includes => :owner).to_a pets = Pet.all.merge!(:includes => :owner).to_a
assert_equal 3, pets.length assert_equal 4, pets.length
end end
def test_eager_association_loading_with_belongs_to def test_eager_association_loading_with_belongs_to
......
...@@ -583,7 +583,7 @@ def test_has_many_association_through_a_belongs_to_association ...@@ -583,7 +583,7 @@ def test_has_many_association_through_a_belongs_to_association
end end
def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys
assert_equal 1, owners(:blackbeard).toys.count assert_equal 2, owners(:blackbeard).toys.count
end end
def test_find_on_has_many_association_collection_with_include_and_conditions def test_find_on_has_many_association_collection_with_include_and_conditions
...@@ -882,6 +882,12 @@ def test_has_many_through_with_polymorphic_source ...@@ -882,6 +882,12 @@ def test_has_many_through_with_polymorphic_source
assert_equal [tags(:general)], post.reload.tags assert_equal [tags(:general)], post.reload.tags
end end
def test_has_many_through_obeys_order_on_through_association
owner = owners(:blackbeard)
assert owner.toys.to_sql.include?("pets.name desc")
assert_equal ["parrot", "bulbul"], owner.toys.map { |r| r.pet.name }
end
test "has many through associations on new records use null relations" do test "has many through associations on new records use null relations" do
person = Person.new person = Person.new
......
...@@ -12,3 +12,8 @@ mochi: ...@@ -12,3 +12,8 @@ mochi:
pet_id: 3 pet_id: 3
name: mochi name: mochi
owner_id: 2 owner_id: 2
bulbul:
pet_id: 4
name: bulbul
owner_id: 1
...@@ -2,7 +2,13 @@ bone: ...@@ -2,7 +2,13 @@ bone:
toy_id: 1 toy_id: 1
name: Bone name: Bone
pet_id: 1 pet_id: 1
doll: doll:
toy_id: 2 toy_id: 2
name: Doll name: Doll
pet_id: 2 pet_id: 2
bulbuli:
toy_id: 3
name: Bulbuli
pet_id: 4
class Owner < ActiveRecord::Base class Owner < ActiveRecord::Base
self.primary_key = :owner_id self.primary_key = :owner_id
has_many :pets has_many :pets, -> { order 'pets.name desc' }
has_many :toys, :through => :pets has_many :toys, :through => :pets
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册