| Commit message (Collapse) | Author | Age | Files | Lines |
| |
|
| |
|
| |
|
|
|
|
| |
argument
|
|
|
|
|
|
|
|
| |
This change was necessary because the whitelist wouldn't work.
It would be painful for users trying to update their applications.
This blacklist intent to prevent odd bugs and confusion in code that call mutator
methods directely on the `Relation`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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")
|
|
|
|
| |
won't last - aim to switch back to a blacklist for mutator methods.
|
| |
|
| |
|
|\
| |
| | |
`delgated` => `delegated`
|
| | |
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
I'm pretty confused about the addition of this method. The documentation
says that it was intended to allow the removal of values from the
default scope (in contrast to #except). However it behaves exactly the
same as except: https://gist.github.com/jonleighton/7537008 (other than
having a slightly enhanced syntax).
The removal of the default scope is allowed by
94924dc32baf78f13e289172534c2e71c9c8cade, which was not a change we
could make until 4.1 due to the need to deprecate things. However after
that change #unscope still gives us nothing that #except doesn't already
give us.
However there *is* a desire to be able to unscope stuff in a way that
persists across merges, which would allow associations to be defined
which unscope stuff from the default scope of the associated model. E.g.
has_many :comments, -> { unscope where: :trashed }
So that's what this change implements. I've also corrected the
documentation. I removed the guide references to #except as I think
unscope really supercedes #except now.
While we're here, there's also a potential desire to be able to write
this:
has_many :comments, -> { unscoped }
However, it doesn't make sense and would not be straightforward to
implement. While with #unscope we're specifying exactly what we want to
be removed from the relation, with "unscoped" we're just saying that we
want it to not have some things which were added earlier on by the
default scope. However in the case of an association, we surely don't
want *all* conditions to be removed, otherwise the above would just
become "SELECT * FROM comments" with no foreign key constraint.
To make the above work, we'd have to somehow tag the relation values
which get added when evaluating the default scope in order to
differentiate them from other relation values. Which is way too much
complexity and therefore not worth it when most use cases can be
satisfied with unscope.
Closes #10643, #11061.
|
|/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
This fixes a bug when merging relations of different classes.
```
Given:
Post.joins(:author).merge(Author.order(name: :desc)).to_sql
Before:
SELECT "posts".* FROM "posts"
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
ORDER BY "posts"."name" DESC
After:
SELECT "posts".* FROM "posts"
INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
ORDER BY "authors"."name" DESC
```
|
| |
|
| |
|
| |
|
| |
|
|
|
|
| |
addresses "ArgumentError: comparison of VerySpecialComment with SpecialComment failed" in ActiveRecord::DelegationRelationTest#test_#sort!_delegation_is_deprecated
|
|
|
|
| |
Without this, some tests here were not actually testing anything.
|
|
|
|
| |
[ci skip]
|
| |
|
|
|
|
| |
named where condition.
|
|
|
|
|
|
|
|
| |
Every method from MRI's core classes is written in C. This means
Method#arity always returns -1 for methods with a variable number of
arguments. This is not the case with Rubinius, where, for example
Array#slice! is implemented in Ruby and has arity -2, since is
defined as def slice!(start, length = undefined)
|
|\
| |
| |
| |
| |
| |
| |
| | |
Deprecate the delegation of Array bang methods in ActiveRecord::Delegation
Conflicts:
activerecord/CHANGELOG.md
activerecord/test/cases/relation_test.rb
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
The primary means of returning results for Array bang methods is to modify
the array in-place. When you call these methods on a relation, that
array is created, modified, and then thrown away. Only the secondary
return value is exposed to the caller.
Removing this delegation is a straight-forward way to reduce user error
by forcing callers to first explicitly call #to_a in order to expose
the array to be acted on by the bang method.
|
|/
|
|
| |
cases/relation/mutation_test.
|
|
|
|
| |
add changelog entry for #11945
|
|
|
|
|
| |
tesetcases assertion to case insensitive because Oracle database adapter
handles table name in uppercase.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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*
|
| |
|
|
|
|
|
| |
This reverts commit 408227d9c5ed7de26310d72a1a99c1ee02311c63, reversing
changes made to dca0b57d03deffc933763482e615c3cf0b9a1d97.
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| |
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
|
|
|
|
|
| |
This reverts commit 88cc1688d0cb828c17706b41a8bd27870f2a2beb, reversing
changes made to f049016cd348627bf8db0d72382d7580bf802a79.
|
|
|
|
|
|
|
|
|
|
|
|
| |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
The real win with these chain methods is where.not, that takes care of
different scenarios in a graceful way, for instance when the given value
is nil.
where("author.id != ?", author_to_ignore.id)
where.not("author.id", author_to_ignore.id)
Both where.like and where.not_like compared to the SQL versions doesn't
seem to give us that much:
Post.where("title LIKE 'ruby on%'")
Post.where.like(title: 'ruby on%'")
Post.where("title NOT LIKE 'ruby on%'")
Post.where.not_like(title: 'ruby on%'")
Thus Rails is adding where.not, but not where.like/not_like and others.
|
|
|
|
|
|
|
|
| |
This commit stems from https://github.com/rails/rails/pull/8332#issuecomment-11127957
Since the formats in which conditions can be passed to `not` differ
from the formats in which conditions can be passed to `like` and `not_like`,
then I think it's worth adding rdoc and tests to show this behavior
|
|
|
|
|
|
| |
Arel::Nodes::In inherits from Arel::Nodes::Equality, so the case
statement was always using the Equality operator for both scenarios,
resulting in a not equal query instead.
|
|
|
|
| |
This test does not belong to has many associations test.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
examples:
Model.where.not field: nil
#=> "SELECT * FROM models WHERE field IS NOT NULL
Model.where.like name: 'Jeremy%'
#=> "SELECT * FROM models WHERE name LIKE 'Jeremy%'
this feature was originally suggested by Jeremy Kemper https://github.com/rails/rails/pull/5950#issuecomment-5591330
Closes #5950
|
|
|
|
| |
Closes #6960
|
|
|
|
|
| |
Previously the reflection would be looked up on the wrong class. However
the test passed because the examples referred back to themselves.
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
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)
|