aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorJason Meller <jason.meller@mandiant.com>2014-01-21 17:34:39 -0500
committerJason Meller <jason.meller@mandiant.com>2014-01-21 19:35:27 -0500
commit03855e790de2224519f55382e3c32118be31eeff (patch)
tree66e881ca9c3d960058edaea67d1bbbc5d60b3d92 /activerecord/lib/active_record/relation
parent9383de42a2cf23cc53052cec2f736864c1c562a1 (diff)
downloadrails-03855e790de2224519f55382e3c32118be31eeff.tar.gz
rails-03855e790de2224519f55382e3c32118be31eeff.tar.bz2
rails-03855e790de2224519f55382e3c32118be31eeff.zip
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where newly created finder methods #second, #third, #forth, and #fifth caused a NoMethodError error on reload associations and where we were pulling the wrong element out of cached associations. Examples: some_book.authors.reload.second # Before # => NoMethodError: undefined method 'first' for nil:NilClass # After # => #<Author id: 2, name: "Sally Second", ...> some_book.first.authors.first some_book.first.authors.second # Before # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 1, name: "Freddy First", ...> # After # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 2, name: "Sally Second", ...> Fixes #13783.
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb16
1 files changed, 8 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index f2ac351a8b..2dd1e6f14b 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -129,7 +129,7 @@ module ActiveRecord
if limit
find_nth_with_limit(offset_value, limit)
else
- find_nth(offset_value)
+ find_nth(:first, offset_value)
end
end
@@ -179,7 +179,7 @@ module ActiveRecord
# Person.offset(3).second # returns the second object from OFFSET 3 (which is OFFSET 4)
# Person.where(["user_name = :u", { u: user_name }]).second
def second
- find_nth(offset_value ? offset_value + 1 : 1)
+ find_nth(:second, offset_value ? offset_value + 1 : 1)
end
# Same as +second+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -195,7 +195,7 @@ module ActiveRecord
# Person.offset(3).third # returns the third object from OFFSET 3 (which is OFFSET 5)
# Person.where(["user_name = :u", { u: user_name }]).third
def third
- find_nth(offset_value ? offset_value + 2 : 2)
+ find_nth(:third, offset_value ? offset_value + 2 : 2)
end
# Same as +third+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -211,7 +211,7 @@ module ActiveRecord
# Person.offset(3).fourth # returns the fourth object from OFFSET 3 (which is OFFSET 6)
# Person.where(["user_name = :u", { u: user_name }]).fourth
def fourth
- find_nth(offset_value ? offset_value + 3 : 3)
+ find_nth(:fourth, offset_value ? offset_value + 3 : 3)
end
# Same as +fourth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -227,7 +227,7 @@ module ActiveRecord
# Person.offset(3).fifth # returns the fifth object from OFFSET 3 (which is OFFSET 7)
# Person.where(["user_name = :u", { u: user_name }]).fifth
def fifth
- find_nth(offset_value ? offset_value + 4 : 4)
+ find_nth(:fifth, offset_value ? offset_value + 4 : 4)
end
# Same as +fifth+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -243,7 +243,7 @@ module ActiveRecord
# Person.offset(3).forty_two # returns the fifth object from OFFSET 3 (which is OFFSET 44)
# Person.where(["user_name = :u", { u: user_name }]).forty_two
def forty_two
- find_nth(offset_value ? offset_value + 41 : 41)
+ find_nth(:forty_two, offset_value ? offset_value + 41 : 41)
end
# Same as +forty_two+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
@@ -444,9 +444,9 @@ module ActiveRecord
end
end
- def find_nth(offset)
+ def find_nth(ordinal, offset)
if loaded?
- @records.first
+ @records.send(ordinal)
else
@offsets[offset] ||= find_nth_with_limit(offset, 1).first
end