aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/finder_methods.rb
Commit message (Collapse)AuthorAgeFilesLines
* [ci skip] Fix the FinderMethods#find documentakihiro172016-03-141-2/+2
| | | | We should use #find_or_initialize_by and #find_or_create_by because #first_or_initialize and #first_or_create methods are not the public API
* default second_to_last to primary_key index if no order suppliedBrian Christian2016-02-271-1/+7
|
* refactor AR second_to_last to use array methodsBrian Christian2016-02-271-1/+6
|
* AR #second_to_last tests and finder methodsBrian Christian2016-02-271-4/+12
|
* Mutating the result of Relation#to_a should not affect the relationMatthew Draper2016-02-211-3/+3
| | | | | | Clarifying this separation and enforcing relation immutability is the culmination of the previous efforts to remove the mutator method delegations.
* Remove the method already overridden by private methodMehmet Emin İNAÇ2016-02-141-13/+0
|
* Merge pull request #23377 from bogdan/last-with-sqlEileen M. Uchitelle2016-02-131-9/+19
|\ | | | | Fix AR::Relation#last bugs instroduced in 7705fc
| * Make ActiveRecord::Relation#last to reverse SQL orderBogdan Gusiev2016-02-131-9/+19
| | | | | | | | instead of loading the relation into memory
* | Fix grammar `a` to `an` [ci skip]Ryuta Kamizono2016-02-131-1/+1
|/
* remove warnings from FinderMethodsyuuji.yaginuma2016-02-111-4/+4
| | | | | | | | | | | This removes the following warnings. ``` activerecord/lib/active_record/relation/finder_methods.rb:252: warning: ambiguous first argument; put parentheses or a space even after `-' operator activerecord/lib/active_record/relation/finder_methods.rb:258: warning: ambiguous first argument; put parentheses or a space even after `-' operator activerecord/lib/active_record/relation/finder_methods.rb:268: warning: ambiguous first argument; put parentheses or a space even after `-' operator activerecord/lib/active_record/relation/finder_methods.rb:274: warning: ambiguous first argument; put parentheses or a space even after `-' operator ```
* rename to 'second_to_last' and 'third_to_last'Brian Christian2016-02-101-12/+12
|
* allow Array.penultimate and Array.antepenultiate access methodsBrian Christian2016-02-091-0/+32
|
* Extract a Relation#arel_attributeMatthew Draper2016-02-041-2/+2
|
* Defer Arel attribute lookup to the model classMatthew Draper2016-02-041-2/+2
| | | | | This still isn't as separated as I'd like, but it at least moves most of the burden of alias mapping in one place.
* Revert "Merge pull request #16400 from bogdan/last-with-sql"Sean Griffin2016-02-011-17/+22
| | | | | | | | | | This reverts commit 9f3730a516f30beb0050caea9539f8d6b808e58a, reversing changes made to 2637fb75d82e1c69333855abd58c2470994995d3. There are additional issues with this commit that need to be addressed before this change is ready (see #23377). This is a temporary revert in order for us to have more time to address the issues with that PR, without blocking the release of beta2.
* Reworked ActiveRecord::Relation#last to always use SQLBogdan Gusiev2016-01-281-22/+17
| | | | instead of loading relation
* Consistently warn that passing an offset to `find_nth` is deprecatedBen Woosley2016-01-271-11/+10
| | | | | | | | | | | | @bogdan pointed out that a `loaded?` relation would not warn that the supplied offset would be removed. This fixes that oversight. https://github.com/rails/rails/commit/16a476e4f8f802774ae7c8dca2e59f4e672dc591#commitcomment-15706834 Although this second argument is probably not widely used, and would be ignored anyway in the loaded? case, this could protect callers from gotchas. [Ben Woosley & Victor Kmita]
* Fix `first(limit)` to take advantage of `loaded?` records if availableBen Woosley2015-12-241-5/+20
| | | | | | | | | | | | | | | | | I realized that `first(2)`, etc. was unnecessarily querying for the records when they were already preloaded. This was because `find_nth_with_limit` can not know which `@records` to return because it conflates the `offset` and `index` into a single variable, while the `@records` only needs the `index` itself to select the proper record. Because `find_nth` and `find_nth_with_limit` are public methods, I instead introduced a private method `find_nth_with_limit_and_offset` which is called internally and handles the `loaded?` checking. Once the `offset` argument is removed from `find_nth`, `find_nth_with_limit_and_offset` can be collapsed into `find_nth_with_limit`, with `offset` always equal to `offset_index`.
* Deprecate passing `offset` to `find_nth`Ben Woosley2015-12-241-8/+16
| | | | | | | | All uses of the `offset` are passing `offset_index`. Better to push down the `offset` consideration into `find_nth`. This also works toward enabling `find_nth_with_limit` to take advantage of the `loaded?` state of the relation.
* Merge pull request #22653 from matthewd/find_array_orderedMatthew Draper2015-12-181-0/+17
|\ | | | | | | ActiveRecord::Base#find(array) returning result in the same order as the array passed
| * Implement limit & offset for ourselvesMatthew Draper2015-12-181-6/+19
| | | | | | | | | | | | | | | | | | | | We know the query will return exactly one row for each entry in the `ids` array, so we can do all the limit/offset calculations on that array, in advance. I also split our new ordered-ids behaviour out of the existing `find_some` method: especially with this change, the conditionals were overwhelming the actual logic.
| * using order_values method instead of relying on ActiveRecord::Relation ↵Miguel Grazziotin2015-06-191-2/+2
| | | | | | | | @values hash
| * WIP: fixing the limit bug and introducing new tests (failing for now) on ↵Miguel Grazziotin2015-06-191-3/+5
| | | | | | | | .find(array) with offset
| * do not change the order of the result if the object was already ordered by ↵Miguel Grazziotin2015-06-051-0/+1
| | | | | | | | the user via :order clause
| * [#20338] improving implementation, fixing and adding some more specsMiguel Grazziotin2015-06-031-5/+2
| |
| * [#20338] WIP: first basic implementation and specsMiguel Grazziotin2015-05-291-1/+5
| |
* | Revert "Perform a more efficient query in `Relation#any?`"Sean Griffin2015-12-141-7/+2
| | | | | | | | | | | | | | | | | | This reverts commit 6d5b1fdf55611de2a1071c37544933bb588ae88e. `eager_load` and `references` can include hashes, which won't match up with `references` A test case has been added to demonstrate the problem
* | Perform a more efficient query in `Relation#any?`Sean Griffin2015-12-141-2/+7
| | | | | | | | | | | | | | | | | | | | | | This was changed in 421c81b, as `exists?` blows up if you are eager loading a polymorphic association, as it'll try to construct a join to that table. The previous change decided to execute a `count` instead, which wouldn't join. Of course, the only time we actually need to perform a join on the eager loaded values (which would perform a left outer join) is if they're being referenced in the where clause. This doesn't affect inner joins.
* | applies new doc guidelines to Active Record.Yves Senn2015-10-141-20/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* | Extra caller details added to ActiveRecord::RecordNotFoundSameer Rahmani2015-07-211-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ActiveRecord::RecordNotFound modified to store model name, primary_key and id of the caller model. It allows the catcher of this exception to make a better decision to what to do with it. For example consider this simple example: class SomeAbstractController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :redirect_to_404 private def redirect_to_404(e) return redirect_to(posts_url) if e.model == 'Post' raise end end
* | Include `Enumerable` in `ActiveRecord::Relation`Sean Griffin2015-06-191-5/+2
|/ | | | | | | | | | | After discussing, we've decided it makes more sense to include it. We're already forwarding every conflicting method to `to_a`, and there's no conflation of concerns. `Enumerable` has no mutating methods, and it just allows us to simplify the code. No existing methods will have a change in behavior. Un-overridden Enumerable methods will simply delegate to `each`. [Sean Griffin & bogdan]
* [ci skip] Remove comments about Rails 3.1claudiob2015-05-111-3/+3
| | | | | | | | | Stems from https://github.com/rails/rails/pull/20105#issuecomment-100900939 where @senny said: > From my point of view, all the docs (guides, API) are version bound. > They should describe that version and continue to be available when newer versions are released. > The cross referencing can be done by the interested user.
* [ci skip] Stop explaining finders for Rails 3claudiob2015-05-101-14/+2
| | | | | Now that master points at Rails 5, we might not need to explain how things used to work in Rails 3. Or we might… up to you :grin:
* Raise ArgumentError when find_by receives no argumentsKohei Suzuki2015-04-111-4/+4
| | | | | | | | | It fixes the strange error saying undefined method `take'. ``` RelationTest#test_find_by_without_arg_behaves_same_with_find_by({}): NoMethodError: undefined method `take' for #<ActiveRecord::QueryMethods::WhereChain:0x007f9c55db1d68> ```
* [ci skip] Combine complementary AR #find doc linesAlexander Dimitriyadi2015-03-301-3/+1
|
* Fix referencing wrong aliases while joining tables of has many throughpinglamb2015-03-221-1/+1
| | | | | | | | | | | association While joining table of has_many :through association, ActiveRecord will use the actual table name instead of through-join alias. It results with a wrong SQL and exception is raised. This only happens when calculation methods like #count is called. This issue is affecting Rails 4.1.x and 4.2.x as well.
* Removed non-standard and unused require 'active_support/deprecation' from ↵Vipul A M2015-02-271-1/+0
| | | | parts out of active_support.
* Remove Relation#bind_paramsSean Griffin2015-01-271-3/+3
| | | | | | | | `bound_attributes` is now used universally across the board, removing the need for the conversion layer. These changes are mostly mechanical, with the exception of the log subscriber. Additional, we had to implement `hash` on the attribute objects, so they could be used as a key for query caching.
* Unify access to bind values on RelationSean Griffin2015-01-271-3/+3
| | | | | | | | | | | | | | | | | | | The bind values can come from four places. `having`, `where`, `joins`, and `from` when selecting from a subquery that contains binds. These need to be kept in a specific order, since the clauses will always appear in that order. Up until recently, they were not. Additionally, `joins` actually did keep its bind values in a separate location (presumably because it's the only case that people noticed was broken). However, this meant that anything accessing just `bind_values` was broken (which most places were). This is no longer possible, there is only a single way to access the bind values, and it includes joins in the proper location. The setter was removed yesterday, so breaking `+=` cases is not possible. I'm still not happy that `joins` is putting it's bind values on the Arel AST, and I'm planning on refactoring it further, but this removes a ton of bug cases.
* Don't remove join dependencies in `Relation#exists?`Sean Griffin2015-01-231-1/+1
| | | | Fixes #18632
* Go through normal where logic in `apply_join_dependency`Sean Griffin2014-12-261-1/+1
| | | | Part of the larger refactoring to remove type casting from Arel.
* Update Arel usage for rails/arel#98fc259Sean Griffin2014-11-291-4/+4
| | | | | `where_sql` now requires that we pass it an engine. None of the manager classes take an engine in their constructor.
* Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-291-1/+1
|
* Handle `RangeError` from casting in `find_by` and `find_by!` on RelationSean Griffin2014-11-021-0/+4
| | | | | We should not behave differently just because a class has a default scope.
* Don't duplicate predicate building logic in Relation findersSean Griffin2014-11-011-5/+2
|
* Treat strings greater than int max value as out of rangeSean Griffin2014-10-311-0/+2
| | | | | | | | | | | Sufficiently large integers cause `find` and `find_by` to raise `StatementInvalid` instead of `RecordNotFound` or just returning `nil`. Given that we can't cast to `nil` for `Integer` like we would with junk data for other types, we raise a `RangeError` instead, and rescue in places where it would be highly unexpected to get an exception from casting. Fixes #17380
* let's warn with heredocsXavier Noria2014-10-281-4/+9
| | | | | | | | | | | | The current style for warning messages without newlines uses concatenation of string literals with manual trailing spaces where needed. Heredocs have better readability, and with `squish` we can still produce a single line. This is a similar use case to the one that motivated defining `strip_heredoc`, heredocs are super clean.
* Remove duplicate error message "Couldn't find..."claudiob2014-10-131-6/+10
| | | | | | | | | This commit removes the duplication of the error message: > Couldn't find #{@klass.name} with [#{arel.where_sql}] introduced in #15791 by adding a private method `find_nth!` that deals with all the method like `first!` and `second!`.
* Merge pull request #15791 from zev/add_model_to_recordnotfound_messageAaron Patterson2014-09-191-8/+8
|\ | | | | Update RecordNotFound exception cases to include a message with the
| * Update RecordNotFound exception cases to include a message with theZev Blut2014-06-181-8/+8
| | | | | | | | Model that the Record was not found in.