aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-2/+2
| | | | | | | | | Fix habtm reflection Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb activerecord/lib/active_record/reflection.rb activerecord/test/cases/reflection_test.rb
* Give real privacy to class methods in AR::PredicateBuilderHector Satre2014-05-011-7/+8
|
* Better support for `where()` conditions that use an association name.Martin Emde2013-12-161-3/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using the name of an association in `where` previously worked only if the value was a single `ActiveRecrd::Base` object. e.g. Post.where(author: Author.first) Any other values, including `nil`, would cause invalid SQL to be generated. This change supports arguments in the `where` query conditions where the key is a `belongs_to` association name and the value is `nil`, an `Array` of `ActiveRecord::Base` objects, or an `ActiveRecord::Relation` object. # Given the Post model class Post < ActiveRecord::Base belongs_to :author end # nil value finds records where the association is not set Post.where(author: nil) # SELECT "posts".* FROM "posts" WHERE "posts"."author_id" IS NULL # Array values find records where the association foreign key # matches the ids of the passed ActiveRecord models, resulting # in the same query as Post.where(author_id: [1,2]) authors_array = [Author.find(1), Author.find(2)] Post.where(author: authors_array) # ActiveRecord::Relation values find records using the same # query as Post.where(author_id: Author.where(last_name: "Emde")) Post.where(author: Author.where(last_name: "Emde")) Polymorphic `belongs_to` associations will continue to be handled appropriately, with the polymorphic `association_type` field added to the query to match the base class of the value. This feature previously only worked when the value was a single `ActveRecord::Base`. class Post < ActiveRecord::Base belongs_to :author, polymorphic: true end Post.where(author: Author.where(last_name: "Emde")) # Generates a query similar to: Post.where(author_id: Author.where(last_name: "Emde"), author_type: "Author")
* check class hierarchy with is_a? in PredicateBuilder.expandMikhail Dieterle2013-08-271-1/+1
| | | | add changelog entry for #11945
* Add ability to specify how a class is converted to Arel predicatesgrif2013-07-281-35/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This adds the ability for rails apps or gems to have granular control over how a domain object is converted to sql. One simple use case would be to add support for Regexp. Another simple case would be something like the following: class DateRange < Struct.new(:start, :end) def include?(date) (start..end).cover?(date) end end class DateRangePredicate def call(attribute, range) attribute.in(range.start..range.end) end end ActiveRecord::PredicateBuilder.register_handler(DateRange, DateRangePredicate.new) More complex cases might include taking a currency object and converting it from EUR to USD before performing the query. By moving the existing handlers to this format, we were also able to nicely refactor a rather nasty method in PredicateBuilder.
* resolve aliases before passing the hash to the predicate builderAaron Patterson2013-07-021-4/+10
|
* we don't need to to_s the columnAaron Patterson2013-07-011-1/+1
|
* the data structure used to store attribute aliases should not be exposedAaron Patterson2013-07-011-2/+2
|
* Handle aliased attributes in ActiveRecord::Relation.Godfrey Chan2013-05-011-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | When using symbol keys, ActiveRecord will now translate aliased attribute names to the actual column name used in the database: With the model class Topic alias_attribute :heading, :title end The call Topic.where(heading: 'The First Topic') should yield the same result as Topic.where(title: 'The First Topic') This also applies to ActiveRecord::Relation::Calculations calls such as `Model.sum(:aliased)` and `Model.pluck(:aliased)`. This will not work with SQL fragment strings like `Model.sum('DISTINCT aliased')`. Github #7839 *Godfrey Chan*
* stop calling to_sym when building arel nodes [CVE-2013-1854]Aaron Patterson2013-03-151-1/+1
|
* Revert "Merge pull request #9207 from dylanahsmith/mysql-quote-numeric"Steve Klabnik2013-02-271-5/+0
| | | | | This reverts commit 408227d9c5ed7de26310d72a1a99c1ee02311c63, reversing changes made to dca0b57d03deffc933763482e615c3cf0b9a1d97.
* Use IN operator like arel for empty hash in where clauserobertomiranda2013-02-091-1/+1
|
* Reverting e170014113 (Change behaviour with empty hash in where clause)Guillermo Iguaran2013-02-081-1/+1
|
* Reverting 16f6f25 (Change behaviour with empty array in where clause)Guillermo Iguaran2013-02-081-2/+0
|
* Change behaviour with empty array in where clauserobertomiranda2013-02-081-0/+2
|
* Change behaviour with empty hash in where clauserobertomiranda2013-02-081-1/+1
|
* active_record: Quote numeric values compared to string columns.Dylan Smith2013-02-071-0/+5
|
* reduce the number of queries on IN clauses, fix relation queries in `where`Aaron Patterson2013-01-241-1/+1
|
* stop converting strings to symbolsAaron Patterson2013-01-241-1/+1
|
* Refactor predicate builder when receiving empty hashCarlos Antonio da Silva2013-01-171-4/+4
| | | | | | | There's no need to create a new arel table or reflect on the column association if the value is empty, these attributes are not used. Also no need to concat a new array, just append the query value.
* 1.9 Syntax related changesAvnerCohen2012-11-101-2/+2
|
* Remove ActiveRecord::ModelJon Leighton2012-10-261-3/+3
| | | | | | | | | | In the end I think the pain of implementing this seamlessly was not worth the gain provided. The intention was that it would allow plain ruby objects that might not live in your main application to be subclassed and have persistence mixed in. But I've decided that the benefit of doing that is not worth the amount of complexity that the implementation introduced.
* rename AR::Model::Tag to AR::Tag - fixes #7714Francesco Rodriguez2012-09-201-1/+1
|
* fix querying with an empty hashDamien Mathieu2012-09-191-2/+6
| | | | Closes #6960
* Pass in the model class rather than engineJon Leighton2012-09-131-5/+5
| | | | | | | | | In some circumstances engine was Arel::Table.engine which for separate reasons was an ActiveRecord::Model::DeprecationProxy, which caused a deprecation warning. In any case, we want the actual model class here, since we want to use it to infer information about associations.
* Refactor to remove some duplicationJon Leighton2012-09-121-37/+20
|
* Fix nested association referencesJon Leighton2012-09-121-3/+4
| | | | | Previously the reflection would be looked up on the wrong class. However the test passed because the examples referred back to themselves.
* Accept belongs_to assoc. keys in ActiveRecord queriesbeerlington2012-09-111-4/+48
| | | | | | | | | | | | | Allows you to specify the model association key in a belongs_to relationship instead of the foreign key. The following queries are now equivalent: Post.where(:author_id => Author.first) Post.where(:author => Author.first) PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) PriceEstimate.where(:estimate_of => treasure)
* Remove conditional committed by accidentSantiago Pastorino2012-06-091-1/+1
|
* Use each_with_object instead of each hereSantiago Pastorino2012-06-091-1/+1
|
* predicate builder should not recurse for determining where columns.Aaron Patterson2012-05-301-1/+1
| | | | | | Thanks to Ben Murphy for reporting this CVE-2012-2661
* CollectionProxy < RelationJon Leighton2012-05-111-3/+3
| | | | | | | | | | | | | | | | | | | | | This helps bring the interfaces of CollectionProxy and Relation closer together, and reduces the delegation backflips we need to perform. For example, first_or_create is defined thus: class ActiveRecord::Relation def first_or_create(...) first || create(...) end end If CollectionProxy < Relation, then post.comments.first_or_create will hit the association's #create method which will actually add the new record to the association, just as post.comments.create would. With the previous delegation, post.comments.first_or_create expands to post.comments.scoped.first_or_create, where post.comments.scoped has no knowledge of the association.
* Remove Arel::Relation constant from PredicateBuilder.Juanjo Bazán2012-03-271-2/+2
|
* Refactor and cleanup in some ActiveRecord modulesCarlos Antonio da Silva2012-03-031-14/+13
| | | | | | | | | | | * Avoid double hash lookups in AR::Reflection when reflecting associations/aggregations * Minor cleanups: use elsif, do..end, if..else instead of unless..else * Simplify DynamicMatchers#respond_to? * Use "where" instead of scoped with conditions hash * Extract `scoped_by` method pattern regexp to constant * Extract noisy class_eval from method_missing in dynamic matchers * Extract readonly check, avoid calling column#to_s twice in persistence * Refactor predicate builder, remove some variables
* automatically add references when we canJon Leighton2012-01-161-0/+12
|
* refactor AR::PredicateBuilder.build_from_hashAkira Matsuda2011-12-291-35/+37
|
* where(foo: [1, nil]) becomes "WHERE foo = 1 OR foo IS NULL"Akira Matsuda2011-12-281-1/+4
| | | | was "WHERE foo IN (1) OR foo IS NULL" before
* no need to compact an already compacted ArrayAkira Matsuda2011-12-281-1/+1
|
* Make PredicateBuilder recognise AR::ModelJon Leighton2011-12-241-2/+2
|
* pushing caching and visitors down to the connectionAaron Patterson2011-11-191-1/+1
|
* Fixes issue #3483, regarding using a mixture of ranges and discrete values ↵Ryan Naughton2011-11-141-6/+8
| | | | in find conditions. Paired with Joey Schoblaska.
* Fix PredicateBuilder clobbering select_values in subquery.Ernie Miller2011-08-201-1/+1
|
* Fix assumption of primary key name in PredicateBuilder subquery.Ernie Miller2011-08-201-1/+1
|
* supporting nil when passed in as an IN clauseAaron Patterson2011-04-291-1/+12
|
* use Arel::Table#alias rather than passing the :as parameterAaron Patterson2011-03-051-2/+2
|
* Split AssociationProxy into an Association class (and subclasses) which ↵Jon Leighton2011-02-181-1/+1
| | | | manages the association, and a CollectionProxy class which is *only* a proxy. Singular associations no longer have a proxy. See CHANGELOG for more.
* use the arel table rather than generating stringsAaron Patterson2011-02-161-1/+1
|
* no need for Array.wrapAaron Patterson2011-02-161-1/+1
|
* removed an unnecessary second query when passing an ActiveRecord::Relation ↵Steven Fenigstein2011-02-161-1/+4
| | | | to a where clause. And added ability to use subselects in where clauses.
* User id instead of quoted_id to prevent double quoting. Fixes failing test ↵Robert Pankowecki (Gavdi)2011-01-041-1/+1
| | | | for bug #6036.