1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Commit graph

26 commits

Author SHA1 Message Date
Melanie Gilman
be6897e34d Remove deprecated behavior allowing nested arrays as query values 2014-12-04 16:13:00 -05:00
Sean Griffin
306d7a43b9 Fix test which failed in isolation
It was transitively relying on the vertex model being loaded
2014-11-01 15:49:12 -06:00
Cristian Bica
72d1663bc7 Fix query with nested array in Active Record
`User.where(id: [[1,2],3])` was equal to `User.where(id:[1, 2, 3])`
in Rails 4.1.x but because of some refactoring in Arel this stopped
working in 4.2.0. This fixes it in Rails.

[Dan Olson & Cristian Bica]
2014-09-06 23:46:32 +03:00
Jean Boussier
6fdf5167b7 Add a test case of nested empty array values in conditions
Post.where(id: [[]]).to_a

Used to fail with a SQL syntax error (until 4.1):

  SELECT ... WHERE id in ();

It now properly generate:

  SELECT ... WHERE 1=0;
2014-08-17 07:55:42 -04:00
Bogdan Gusiev
fe67dfbbee Fixed AR::Relation#where edge case with Hash and other Relation
Example:

  Author.where(posts: { author_id: Author.where(country_id: 1) }).joins(:posts)
2014-08-14 13:44:29 +03:00
Dylan Thacker-Smith
42be84ba40 active_record: Type cast booleans and durations for string columns. 2014-07-06 02:43:34 -04:00
Martin Emde
8062a30794 Better support for where() conditions that use an association name.
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")
2013-12-16 14:16:15 -08:00
David Heinemeier Hansson
661637e5b6 Delegate #rewhere to all on the class like all other relation methods 2013-11-02 19:45:03 -07:00
Mikhail Dieterle
c2e084ac48 check class hierarchy with is_a? in PredicateBuilder.expand
add changelog entry for #11945
2013-08-27 15:47:21 +03:00
Godfrey Chan
54122067ac Handle aliased attributes in ActiveRecord::Relation.
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*
2013-05-01 16:36:01 -07:00
Steve Klabnik
afd4a14332 Revert "Merge pull request #9207 from dylanahsmith/mysql-quote-numeric"
This reverts commit 408227d9c5, reversing
changes made to dca0b57d03.
2013-02-27 08:46:40 -08:00
Guillermo Iguaran
29f3a036e7 Reverting e170014113 (Change behaviour with empty hash in where clause) 2013-02-08 16:53:49 -05:00
Guillermo Iguaran
fa987cb8ec Reverting 16f6f25 (Change behaviour with empty array in where clause) 2013-02-08 14:22:10 -05:00
robertomiranda
16f6f2592e Change behaviour with empty array in where clause 2013-02-08 06:37:30 -05:00
robertomiranda
e170014113 Change behaviour with empty hash in where clause 2013-02-08 01:04:32 -05:00
Dylan Smith
a712e08ebe active_record: Quote numeric values compared to string columns. 2013-02-07 04:59:33 -05:00
Aaron Patterson
170fb5c80c reduce the number of queries on IN clauses, fix relation queries in where 2013-01-24 14:29:03 -08:00
Carlos Antonio da Silva
507d23c421 Fix syntax error and remove duplicated test 2013-01-08 20:00:51 -02:00
Aaron Patterson
8e577fe560 * Strip nils from collections on JSON and XML posts. [CVE-2013-0155] * dealing with empty hashes. Thanks Damien Mathieu
Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/action_dispatch/http/request.rb
	actionpack/lib/action_dispatch/middleware/params_parser.rb
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/predicate_builder.rb
	activerecord/test/cases/relation/where_test.rb
2013-01-08 12:41:24 -08:00
Jeremy Kemper
c31cc963da Revert "Merge branch 'master-sec'"
This reverts commit 88cc1688d0, reversing
changes made to f049016cd3.
2013-01-08 12:41:04 -08:00
Aaron Patterson
d99e8c9e16 * Strip nils from collections on JSON and XML posts. [CVE-2013-0155] * dealing with empty hashes. Thanks Damien Mathieu
Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/action_dispatch/http/request.rb
	actionpack/lib/action_dispatch/middleware/params_parser.rb
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/predicate_builder.rb
	activerecord/test/cases/relation/where_test.rb
2013-01-07 17:20:12 -08:00
Carlos Antonio da Silva
55dec5a7da Move where with blank conditions test to the correct where tests file
This test does not belong to has many associations test.
2012-12-07 01:08:38 -02:00
Damien Mathieu
30a576fa14 fix querying with an empty hash
Closes #6960
2012-09-19 15:57:22 +02:00
Jon Leighton
eb4a623d74 Fix nested association references
Previously the reflection would be looked up on the wrong class. However
the test passed because the examples referred back to themselves.
2012-09-12 23:32:50 +01:00
beerlington
3da275c439 Accept belongs_to assoc. keys in ActiveRecord queries
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)
2012-09-11 14:11:51 -04:00
Aaron Patterson
9340f89849 predicate builder should not recurse for determining where columns.
Thanks to Ben Murphy for reporting this

CVE-2012-2661
2012-05-30 15:09:13 -07:00