aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
Commit message (Collapse)AuthorAgeFilesLines
* Optimize none? and one? relation query methods to use LIMIT and COUNT.Eugene Gilburg2015-02-121-0/+18
| | | | | | | | Use SQL COUNT and LIMIT 1 queries for none? and one? methods if no block or limit is given, instead of loading the entire collection to memory. The any? and many? methods already follow this behavior. [Eugene Gilburg & Rafael Mendonça França]
* Remove Relation#bind_paramsSean Griffin2015-01-271-9/+9
| | | | | | | | `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-6/+4
| | | | | | | | | | | | | | | | | | | 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-261-3/+3
| | | | | | | 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-261-1/+1
| | | | | | | | | | 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.
* Change `having_values` to use the `WhereClause` classSean Griffin2015-01-261-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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]
* Move `where_values_hash` over to `WhereClause`Sean Griffin2015-01-251-16/+1
|
* Introduce `Relation::WhereClause`Sean Griffin2015-01-251-3/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The way that bind values are currently stored on Relation is a mess. They can come from `having`, `where`, or `join`. I'm almost certain that `having` is actually broken, and calling `where` followed by `having` followed by `where` will completely scramble the binds. Joins don't actually add the bind parameters to the relation itself, but instead add it onto an accessor on the arel AST which is undocumented, and unused in Arel itself. This means that the bind values must always be accessed as `relation.arel.bind_values + relation.bind_values`. Anything that doesn't is likely broken (and tons of bugs have come up for exactly that reason) The result is that everything dealing with `Relation` instances has to know far too much about the internals. The binds are split, combined, and re-stored in non-obvious ways that makes it difficult to change anything about the internal representation of `bind_values`, and is extremely prone to bugs. So the goal is to move a lot of logic off of `Relation`, and into separate objects. This is not the same as what is currently done with `JoinDependency`, as `Relation` knows far too much about its internals, and vice versa. Instead these objects need to be black boxes that can have their implementations swapped easily. The end result will be two classes, `WhereClause` and `JoinClause` (`having` will just re-use `WhereClause`), and there will be a single method to access the bind values of a `Relation` which will be implemented as ``` join_clause.binds + where_clause.binds + having_clause.binds ``` This is the first step towards that refactoring, with the internal representation of where changed, and an intermediate representation of `where_values` and `bind_values` to let the refactoring take small steps. These will be removed shortly.
* Don't mutate bind values in `Relation`Sean Griffin2015-01-191-1/+0
| | | | | | | In order to better facilitate refactoring, most places that mutated `bind_values` have already been removed. One last spot snuck through. Since we're no longer mutating the array, it also does not need to be duped in `initialize_copy`.
* Stop passing a column to `quote` in `Relation#to_sql`Sean Griffin2015-01-101-2/+3
| | | | | | | I'm planning on deprecating the column argument to mirror the deprecation in [arel]. [arel]: https://github.com/rails/arel/commit/6160bfbda1d1781c3b08a33ec4955f170e95be11
* Merge pull request #11898 from prathamesh-sonpatki/patch-updateRafael Mendonça França2015-01-021-1/+13
|\ | | | | | | | | | | | | Changed ActiveRecord::Relation#update behavior so that it will work on Relation objects without giving id Conflicts: activerecord/CHANGELOG.md
| * Allow ActiveRecord::Relation#update to run on result of a relation with ↵Prathamesh Sonpatki2014-12-201-1/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks and validations - Right now, there is no method to update multiple records with validations and callbacks. - Changed the behavior of existing `update` method so that when `id` attribute is not given and the method is called on an `Relation` object, it will execute update for every record of the `Relation` and will run validations and callbacks for every record. - Added test case for validating that the callbacks run when `update` is called on a `Relation`. - Changed test_create_columns_not_equal_attributes test from persistence_test to include author_name column on topics table as it it used in before_update callback. - This change introduces performance issues when a large number of records are to be updated because it runs UPDATE query for every record of the result. The `update_all` method can be used in that case if callbacks are not required because it will only run single UPDATE for all the records.
* | Fix error message when trying to create an associated recordRafael Mendonça França2014-12-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | This error only happens when the foreign key is missing. Before this fix the following exception was being raised: NoMethodError: undefined method `val' for #<Arel::Nodes::BindParam:0x007fc64d19c218> Now the message is: ActiveRecord::UnknownAttributeError: unknown attribute 'foreign_key' for Model.
* | Inject the `PredicateBuilder` into the `Relation` instanceSean Griffin2014-12-261-7/+3
|/ | | | | | | Construction of relations can be a hotspot, we don't want to create one of these in the constructor. This also allows us to do more expensive things in the predicate builder's constructor, since it's created once per AR::Base subclass
* Move PredicateBuilder instantiation to constructorMelanie Gilman2014-12-031-4/+5
| | | | In order to maintain thread safety and prevent race condition from memoization.
* Refactor `PredicateBuilder` from singleton to instanceMelanie Gilman2014-12-021-0/+4
|
* Update Arel usage for rails/arel#98fc259Sean Griffin2014-11-291-2/+2
| | | | | `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-4/+4
|
* Reword documentation for update_allRené van den Berg2014-11-241-8/+1
| | | It now contains a carefully formulated reference to the "current relation" which might help clarify that the receiving will generate its own scope, escaping the need for explicitly referencing `default_scope` which is, after all, just another way of specifying a scope and nothing special.
* Explain that default_scope also influences update_allRené van den Berg2014-11-201-5/+12
| | | | | This was not explicitly stated before and I needed to try it out to be certain. A little explicit statement in the API docs might help here.
* pull the preloader allocation in to a factory methodAaron Patterson2014-11-181-1/+5
|
* Remove the unused second argument to `substitute_at`Sean Griffin2014-11-171-1/+1
| | | | Oh hey, we got to remove some code because of that!
* rm `reorder_bind_params`Sean Griffin2014-11-171-1/+0
| | | | | | Arel handles this for us automatically. Updated tests, as BindParam is no longer a subclass of SqlLiteral. We should remove the second argument to substitute_at entirely, as it's no longer used
* Use a bound parameter for the "id = " portion of update statementsSean Griffin2014-11-011-2/+8
| | | | | | We need to re-order the bind parameters since the AST returned by the relation will have the where statement as the first bp, which breaks on PG.
* Use bind values for joined tables in where statementsSean Griffin2014-11-011-1/+1
| | | | | | | | | | | | | | | In practical terms, this allows serialized columns and tz aware columns to be used in wheres that go through joins, where they previously would not behave correctly. Internally, this removes 1/3 of the cases where we rely on Arel to perform type casting for us. There were two non-obvious changes required for this. `update_all` on relation was merging its bind values with arel's in the wrong order. Additionally, through associations were assuming there would be no bind parameters in the preloader (presumably because the where would always be part of a join) [Melanie Gilman & Sean Griffin]
* Don't needlessly alphabetize columns for insert/updateSean Griffin2014-11-011-4/+3
| | | | | This slightly simplifies the code, and reduces the number of times we need to iterate over the attributes by one.
* delete leftover JoinOperation structswapdisc2014-10-141-2/+0
|
* Clarify `#update_all` doc about value processingNicolas Cavigneaux2014-10-141-1/+2
| | | | | | | | | This clarify the fact that `#update_all` doesn't type-cast passed values and that these values are written as-is in the SQL DB. Fix #17242 [ci skip]
* Move #encode_with to RelationGustavo Beathyate2014-07-151-0/+5
|
* Merge pull request #14971 from versioncontrol/#14785Yves Senn2014-06-061-1/+8
|\ | | | | | | Baseclass becomes! subclass
| * Fix Baseclass becomes! subclass.Edo Balvers2014-05-131-1/+8
| |
* | Fix `Relation#delete_all` inconsistencyLeandro Facchinetti2014-05-161-3/+13
|/ | | | | | | | | When relation scopes include one of `uniq`, `group`, `having` or `offset`, the generated query ignores them and that causes unintended records to be deleted. This solves the issue by restricting the deletion when those scopes are present. rails/rails#11985
* Merge branch 'master' into adequaterecordAaron Patterson2014-04-141-5/+6
|\ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (70 commits) [ci skip] Added link to ruby-lang.org installation. Use the index on hidden field `collection_check_boxes` respects `:index` option for the hidden filed name. docs, double meaning of `serialize` argument. Closes #14284. Just call read_attribute, no need to use `send`. - Fix lingering reference to `:text` instead of the newer `:plain` - Section references `form_tag` instead of the `form_for` used in the example again, read_attribute is public, so just call it read_attribute is public, so we should just call it Disable assest cache store in docs [ci skip] Make counter cache decrementation on destroy idempotent Write the failing test case for concurrent counter cache [ci skip] Use plain underscore instead of "\_". Update documentation to use Rails.application instead Add a changelog entry for #14546 [ci skip] Move tests for deep_dup and duplicable to object directory Missing 'are' in note - [ci skip] CollectionHelpers now accepts a readonly option Fix a few typos [ci skip] Bundle tzinfo-data on :x64_mingw (64-bit Ruby on Windows). don't bother with an offset if the offset is zero ...
| * Make the comparison between 'Relation' and 'AssociationRelation'Lauro Caetano2014-04-121-1/+1
| | | | | | | | consistent.
| * The comparison between `Relation` and `CollectionProxy` should be consistent.Lauro Caetano2014-04-111-0/+2
| | | | | | | | | | | | | | | | | | | | | | Example: author.posts == Post.where(author_id: author.id) # => true Post.where(author_id: author.id) == author.posts # => true Fixes #13506
| * Merge pull request #14711 from ↵Rafael Mendonça França2014-04-111-3/+2
| |\ | | | | | | | | | | | | | | | swoker/activerecord_fix_aggregate_methods_with_select Activerecord fix aggregate methods with select
| | * Fix error for aggregate methods with select, see issue #13648Simon Woker2014-04-101-3/+2
| | |
| * | Merge pull request #12829 from iantropov/issue_insert_via_hmt_scope_3548Rafael Mendonça França2014-04-101-2/+2
| |\ \ | | |/ | |/| | | | | | | | | | | | | Fix insertion of records for hmt association with scope Conflicts: activerecord/CHANGELOG.md
| | * Fix insertion of records for hmt association with scope, fix #3548Ivan Antropov2013-11-171-2/+2
| | |
* | | propogate bind values collected in arel to SQL generationAaron Patterson2014-04-111-1/+2
| | |
* | | remove the bind substitution visitor. to_sql should never return bind valuesAaron Patterson2014-04-091-5/+6
| | |
* | | use the compile method so we do not have to specify the collectors in this caseAaron Patterson2014-04-091-2/+1
| | |
* | | working against arel/collector branchAaron Patterson2014-04-091-1/+2
| | |
* | | where_values should extract the value of the Casted nodeAaron Patterson2014-04-071-1/+7
| | | | | | | | | | | | makes adequaterecord work with arel master.
* | | Merge branch 'master' into adequaterecordAaron Patterson2014-04-071-1/+1
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (122 commits) Rails.application should be set inside before_configuration hook remove check for present? from delete_all Remove useless begin..end Build the reverse_order on its proper method. Use connection-specific bytea escaping Ignore order when doing count. make enums distinct per class Remove unused `subclass_controller_with_flash_type_bar` var from flash test. fix CollectionProxy delete_all documentation Added OS X specific commands to installation guide [ci skip] Recommended using homebrew for installing MySQL and PostgreSQL Fix setup of adding _flash_types test. Use SVG version of travis build status badge [skip ci] W3C CSP document moved to gihub.io URL [ci skip] sprockets-rails was released Fix the test defining the models in the right place Add CHANGELOG entry for #11650 [ci skip] Declare the assets dependency Use sass-rails 4.0.3 Make possible to use sprockets-rails 2.1 add missing parentheses to validates_with documentation [skip ci] ...
| * | Merge pull request #11650 from prathamesh-sonpatki/renameRafael Mendonça França2014-04-041-1/+1
| |\ \ | | | | | | | | | | | | Renamed private methods _create_record and _update_record
| | * | [Active Record] Renamed private methods create_record and update_recordPrathamesh Sonpatki2014-02-201-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | This is to ensure that they are not accidentally called by the app code. They are renamed to _create_record and _update_record respectively. Closes #11645
* | | | Merge branch 'master' into adequaterecordAaron Patterson2014-03-131-1/+10
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (108 commits) make tests pass on Ruby 2.2 Use Sqlite3 adapter in examples use the body proxy to freeze headers just ask the response for the commit status, we do not need to ask the jar only write the jar if the response isn't committed Fix a grammatical error in the i18n guide [ci skip] use method_defined? to check whether or not a method is defined Enhance docs for update_attribute [ci-skip] Change usec to 0 on tests that compare seconds Unit test for mysql quote time usec Changelog entry for mysql56 microseconds Test microsecond on mysql 5.6 MySQL 5.6 and later supports microsecond precision in datetime. [ci skip] Add documentation for original_fullpath. Remove mocking on save, when not necessary comment why we are modifying global state. [ci skip] `change_table` supports `citext`. Follow up to #12523. Removed unnecessary command "application" register OID for PostgreSQL citex datatype [Troy Kruthoff & Lachlan Sylvester] Fixes STI when 2+ levels deep. ...
| * | | `includes` uses SQL parsing when String joins are involved.Yves Senn2014-02-281-1/+10
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a partial revert of 22b3481ba2aa55fad1f9a5db94072312b345fb55. The current implementation of `references_eager_loaded_tables?` needs to know every table involved in the query. With the current API this is not possible without SQL parsing. While a2dab46cae35a06fd5c5500037177492a047c252 deprecated SQL parsing for `includes`. It did not issue deprecation warnings when String joins are involved. This resulted in a breaking change after the deprecated behavior was removed (22b3481ba2aa55fad1f9a5db94072312b345fb55). We will need to rethink the usage of `includes`, `preload` and `eager_load` but for now, this brings back the old *working* behavior.
* | | Merge branch 'master' into adequaterecordAaron Patterson2014-02-171-2/+3
|\| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * master: (311 commits) Add a missing changelog entry for #13981 and #14035 Revert "Fixed plugin_generator test" implements new option :month_format_string for date select helpers [Closes #13618] add factory methods for empty alias trackers guarantee a list in the alias tracker so we can remove a conditional stop exposing table_joins make most parameters to the AliasTracker required make a singleton for AssociationScope pass the association and connection to the scope method pass the tracker down the stack and construct it in the scope method clean up add_constraints signature remove the reflection delegate remove klass delegator remove railties changes. fixes #14054 remove chain delegate remove scope_chain delegate Add verb to sanitization note fix path shown in mailer's templates updated Travis build status image url fix guide active_support_core_extensions. add Note to String#indent [ci skip] ... Conflicts: activerecord/lib/active_record/associations/join_dependency.rb activerecord/test/cases/associations/association_scope_test.rb