Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

has_many through obeys order on through association

fixes #10016
  • Loading branch information...
commit 812469943cc6eddc132d137a0d5929b2c5d0346f 1 parent db113d9
@neerajdotname neerajdotname authored
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## 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
transitioning to new association.
View
1  activerecord/lib/active_record/associations/association_scope.rb
@@ -101,6 +101,7 @@ def add_constraints(scope)
scope.includes! item.includes_values
scope.where_values += item.where_values
+ scope.order_values += (item.order_values - scope.order_values)
end
end
View
2  activerecord/test/cases/associations/eager_test.rb
@@ -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
pets = Pet.all.merge!(:includes => :owner).to_a
- assert_equal 3, pets.length
+ assert_equal 4, pets.length
end
def test_eager_association_loading_with_belongs_to
View
8 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -583,7 +583,7 @@ def test_has_many_association_through_a_belongs_to_association
end
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
def test_find_on_has_many_association_collection_with_include_and_conditions
@@ -882,6 +882,12 @@ def test_has_many_through_with_polymorphic_source
assert_equal [tags(:general)], post.reload.tags
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
person = Person.new
View
5 activerecord/test/fixtures/pets.yml
@@ -12,3 +12,8 @@ mochi:
pet_id: 3
name: mochi
owner_id: 2
+
+bulbul:
+ pet_id: 4
+ name: bulbul
+ owner_id: 1
View
6 activerecord/test/fixtures/toys.yml
@@ -2,7 +2,13 @@ bone:
toy_id: 1
name: Bone
pet_id: 1
+
doll:
toy_id: 2
name: Doll
pet_id: 2
+
+bulbuli:
+ toy_id: 3
+ name: Bulbuli
+ pet_id: 4
View
2  activerecord/test/models/owner.rb
@@ -1,5 +1,5 @@
class Owner < ActiveRecord::Base
self.primary_key = :owner_id
- has_many :pets
+ has_many :pets, -> { order 'pets.name desc' }
has_many :toys, :through => :pets
end
Please sign in to comment.
Something went wrong with that request. Please try again.