aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
Commit message (Collapse)AuthorAgeFilesLines
* Ensure symbols passed to `select` are always quotedSean Griffin2015-05-301-9/+7
| | | | | | | | | | | | | | | | | Our general contract in Active Record is that strings are assumed to be SQL literals, and symbols are assumed to reference a column. If a from clause is given, we shouldn't include the table name, but we should still quote the value as if it were a column. Upon fixing this, the tests were still failing on SQLite. This was because the column name being returned by the query was `"\"join\""` instead of `"join"`. This is actually a bug in SQLite that was fixed a long time ago, but I was using the version of SQLite included by OS X which has this bug. Since I'm guessing this will be a common case for contributors, I also added an explicit check with a more helpful error message. Fixes #20360
* Fix the shadowing warning for `reflection`Roque Pinel2015-05-281-2/+2
|
* Merge pull request #20196 from huoxito/preload-association-and-mergesRafael Mendonça França2015-05-281-1/+23
|\ | | | | Properly append preload / includes args on Merger
| * Properly append preload / includes args on MergerWashington Luiz2015-05-281-1/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Couldn't find other way to get the association name from a given class other than looping through `reflect_on_all_associations` reflections .. Noticed this one while looking at this example: ```ruby class Product < ActiveRecord::Base has_many :variants has_many :translations end class Translation < ActiveRecord::Base belongs_to :product end class Variant < ActiveRecord::Base belongs_to :product end class BugTest < Minitest::Test def test_merge_stuff product = Product.create! name: 'huhu' variant = Variant.create! product_id: product.id Translation.create! locale: 'en', product_id: product.id product_relation = Product.all .preload(:translations) .joins(:translations) .merge(Translation.where(locale: 'en')) .where(name: 'huhu') assert_equal variant, Variant.joins(:product).merge(product_relation).first end end ```
* | Allow Relation#compact using delegationJordan Raine2015-05-281-1/+1
| |
* | deprecate `Relation#uniq` use `Relation#distinct` instead.Yves Senn2015-05-261-1/+3
|/ | | | | | | | | See #9683 for the reasons we switched to `distinct`. Here is the discussion that triggered the actual deprecation #20198. `uniq`, `uniq!` and `uniq_value` are still around. They will be removed in the next minor release after Rails 5.
* Remove redundant require 'set' linesMehmet Emin İNAÇ2015-05-151-1/+0
|
* [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:
* Merge pull request #19546 from DianthuDia/fix_unscope_for_less_thanYves Senn2015-04-281-1/+1
|\ | | | | | | Fix unscope for less than
| * Fix unscope for less thanTAKAHASHI Kazuaki2015-03-271-1/+1
| | | | | | | | | | | | Code such as the following will be corrected. Developer.where(id: -Float::INFINITY...2).unscope(where: :id)
* | Merge pull request #19718 from eagletmt/find_by-without-argYves Senn2015-04-281-4/+4
|\ \ | | | | | | | | | Raise ArgumentError when find_by receives no arguments
| * | 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> ```
* | | Revert "Merge pull request #19755 from yuki24/activerecord/support-for-set"Yves Senn2015-04-151-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 524d40591eaa2f4d007409bfad386f6b107492eb, reversing changes made to 34d3a6095100245283861ef480a54d0643bbee4c. Reasoning behind the revert are in the PR discussion: https://github.com/rails/rails/pull/19755 - This means that types can no longer cast to/from `Set`, and reasonably work with `where` (we already have this problem for `array`/`json` types on pg) - This adds precedent for every other `Enumerable`, and we can't target `Enumerable` directly. - Calling `to_a` on a `Set` is reasonable.
* | | Add support for Set to Relation#whereYuki Nishijima2015-04-131-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | Previously `#where` used to treat `Set`objects as nil, but now it treats them as an array: set = Set.new([1, 2]) Author.where(:id => set) # => SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, 2)
* | | Enhance documentation of pluck with a hint to ids [ci skip]wingfire2015-04-121-3/+5
|/ / | | | | | | | | Documentation is giving an example that can be replaced by a more dry command. Give a hint that ids can be used instead of pluck(:id).
* | [ci skip] Combine complementary AR #find doc linesAlexander Dimitriyadi2015-03-301-3/+1
| |
* | [skip ci] Improve `warn_on_records_fetched` documentationJon Atack2015-03-271-3/+2
|/ | | | | | | | - ‘dection’ -> ‘detection’ - ‘exceeds threshold’ -> ‘exceeds the threshold’ - Other minor improvements.
* Add `config.active_record.warn_on_records_fetched_greater_than` optionJason Nochlin2015-03-251-0/+50
| | | | | | | | | When set to an integer, a warning will be logged whenever a result set larger than the specified size is returned by a query. Fixes #16463 The warning is outputed a module which is prepended in an initializer, so there will be no performance impact if `config.active_record.warn_on_records_fetched_greater_than` is not set.
* 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.
* Drop `references_eager_loaded_tables?` test from `has_include?`Ben Woosley2015-03-171-1/+1
| | | | | | | It is redundant with tests in `eager_loading?`, but for the difference between `includes_values.present?` and `includes_values.any?`, which is a difference without a distinction because `false` has no meaning for `includes`.
* Spell PostgreSQL correctly :elephant:Akira Matsuda2015-02-281-1/+1
| | | | [ci skip]
* Removed non-standard and unused require 'active_support/deprecation' from ↵Vipul A M2015-02-271-1/+0
| | | | parts out of active_support.
* Fix c479480638508c20601af69ca46b5b606c2d5b4d to account for from_value -> ↵Jeremy Kemper2015-02-241-1/+1
| | | | from_clause in bdc5141652770fd227455681cde1f9899f55b0b9
* Merge pull request #18744 from mfazekas/no-table-name-with-fromRafael Mendonça França2015-02-241-5/+9
| | | | Fix appending table_name to select and group when used with subquery (fr...
* `Type#type_cast_from_database` -> `Type#deserialize`Sean Griffin2015-02-171-2/+2
|
* correct method name in deprecation messageyuuji.yaginuma2015-02-181-1/+1
|
* Deprecated passing of `start` value to `find_in_batches` and `find_each` in ↵Vipul A M2015-02-171-17/+31
| | | | favour of `begin_at` value.
* Add an option `end_at` to `find_in_batches`Vipul A M2015-02-091-12/+22
| | | | that complements the `start`parameter to specify where to stop batch processing
* Raise ArgumentError when passing nil to Relation#mergeRafael Mendonça França2015-02-061-1/+1
| | | | | | nil or false should not be valid argument to the merge method. Closes #12264
* Use keyword argument in the find_in_batches APIRafael Mendonça França2015-02-061-11/+7
| | | | | We already validate the keys, so it is better to use the built-in feature to do this
* Respect custom primary keys for associations in `Relation#where`Sean Griffin2015-02-041-1/+21
| | | | | | | | | | While we query the proper columns, we go through normal handling for converting the value to a primitive which assumes it should use the table's primary key. If the association specifies a different value (and we know that we're working with an association), we should use the custom primary key instead. Fixes #18813.
* Attribute assignment and type casting has nothing to do with columnsSean Griffin2015-01-311-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It's finally finished!!!!!!! The reason the Attributes API was kept private in 4.2 was due to some publicly visible implementation details. It was previously implemented by overloading `columns` and `columns_hash`, to make them return column objects which were modified with the attribute information. This meant that those methods LIED! We didn't change the database schema. We changed the attribute information on the class. That is wrong! It should be the other way around, where schema loading just calls the attributes API for you. And now it does! Yes, this means that there is nothing that happens in automatic schema loading that you couldn't manually do yourself. (There's still some funky cases where we hit the connection adapter that I need to handle, before we can turn off automatic schema detection entirely.) There were a few weird test failures caused by this that had to be fixed. The main source came from the fact that the attribute methods are now defined in terms of `attribute_names`, which has a clause like `return [] unless table_exists?`. I don't *think* this is an issue, since the only place this caused failures were in a fake adapter which didn't override `table_exists?`. Additionally, there were a few cases where tests were failing because a migration was run, but the model was not reloaded. I'm not sure why these started failing from this change, I might need to clear an additional cache in `reload_schema_from_cache`. Again, since this is not normal usage, and it's expected that `reset_column_information` will be called after the table is modified, I don't think it's a problem. Still, test failures that were unrelated to the change are worrying, and I need to dig into them further. Finally, I spent a lot of time debugging issues with the mutex used in `define_attribute_methods`. I think we can just remove that method entirely, and define the attribute methods *manually* in the call to `define_attribute`, which would simplify the code *tremendously*. Ok. now to make this damn thing public, and work on moving it up to Active Model.
* Post.all.or(anything) == Post.allSean Griffin2015-01-291-2/+2
|
* Fixed AR::Relation#group method when argument is a SQL reserved keywordBogdan Gusiev2015-01-291-13/+15
|
* Don't error when grouped calculations return 0 recordsSean Griffin2015-01-281-1/+1
| | | | Fixes #18717
* Bring the implementation of Relation#or up to speedSean Griffin2015-01-282-36/+21
|
* Added #or to ActiveRecord::RelationMatthew Draper2015-01-281-0/+59
| | | | | | | Post.where('id = 1').or(Post.where('id = 2')) # => SELECT * FROM posts WHERE (id = 1) OR (id = 2) [Matthew Draper & Gael Muller]
* Remove Relation#bind_paramsSean Griffin2015-01-275-17/+27
| | | | | | | | `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.
* Use an `Attribute` object to represent a bind valueSean Griffin2015-01-274-8/+18
| | | | | | | | | | | The column is primarily used for type casting, which we're trying to separate from the idea of a column. Since what we really need is the combination of a name, type, and value, let's use the object that we already have to represent that concept, rather than this tuple. No consumers of the bind values have been changed, only the producers (outside of tests which care too much about internals). This is *finally* possible since the bind values are now produced from a reasonable number of lcoations.
* Minor refactorings on `Relation#build_joins`Sean Griffin2015-01-271-26/+13
| | | | Attempting to grok this code by refactoring it as I go through it.
* `WhereClause#predicates` does not need to be publicSean Griffin2015-01-271-1/+3
| | | | | | | | | | | The only place it was accessed was in tests. Many of them have another way that they can test their behavior, that doesn't involve reaching into internals as far as they did. `AssociationScopeTest` is testing a situation where the where clause would have one bind param per predicate, so it can just ignore the predicates entirely. The where chain test was primarly duplicating the logic tested on `WhereClause` directly, so I instead just make sure it calls the appropriate method which is fully tested in isolation.
* Use the `WhereClause` ast building logic for havingSean Griffin2015-01-271-4/+1
|
* Move where grouping into `WhereClause`Sean Griffin2015-01-272-11/+26
|
* Unify access to bind values on RelationSean Griffin2015-01-275-15/+9
| | | | | | | | | | | | | | | | | | | 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.
* Move the `from` bind logic to a `FromClause` classSean Griffin2015-01-263-16/+47
| | | | | | | Contrary to my previous commit message, it wasn't overkill, and led to much cleaner code. [Sean Griffin & anthonynavarre]
* Remove `Relation#bind_values=`Sean Griffin2015-01-262-10/+8
| | | | | | | | | | The last place that was assigning it was when `from` is called with a relation to use as a subquery. The implementation was actually completely broken, and would break if you called `from` more than once, or if you called it on a relation, which also had its own join clause, as the bind values would get completely scrambled. The simplest solution was to just move it into its own array, since creating a `FromClause` class for this would be overkill.
* Remove unused `bind` and `bind!` methods from `Relation`Sean Griffin2015-01-261-9/+0
|
* Remove `Relation#build_where`Sean Griffin2015-01-261-6/+0
| | | | All of its uses have been moved to better places
* Change `having_values` to use the `WhereClause` classSean Griffin2015-01-263-5/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | This fixed an issue where `having` can only be called after the last call to `where`, because it messes with the same `bind_values` array. With this change, the two can be called as many times as needed, in any order, and the final query will be correct. However, once something assigns `bind_values`, that stops. This is because we have to move all of the bind values from the having clause over to the where clause since we can't differentiate the two, and assignment was likely in the form of: `relation.bind_values += other.bind_values` This will go away once we remove all places that are assigning `bind_values`, which is next on the list. While this fixes a bug that was present in at least 4.2 (more likely present going back as far as 3.0, becoming more likely in 4.1 and later as we switched to prepared statements in more cases), I don't think this can be easily backported. The internal changes to `Relation` are non-trivial, anything that involves modifying the `bind_values` array would need to change, and I'm not confident that we have sufficient test coverage of all of those locations (when `having` was called with a hash that could generate bind values). [Sean Griffin & anthonynavarre]