aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Added comments about the check_empty_arguments method which is calledwangjohn2013-02-201-0/+16
| | | | | for query methods in a where_clause. Also, modified the CHANGELOG entry because it had false information and added tests.
* Reverting 16f6f25 (Change behaviour with empty array in where clause)Guillermo Iguaran2013-02-081-3/+2
|
* Change behaviour with empty array in where clauserobertomiranda2013-02-081-2/+3
|
* Prevent Relation#merge from collapsing wheres on the RHSJon Leighton2013-01-271-0/+13
| | | | | | | | | | | | | | | | | | | | | | | This caused a bug with the new associations implementation, because now association conditions are represented as Arel nodes internally right up to when the whole thing gets turned to SQL. In Rails 3.2, association conditions get turned to raw SQL early on, which prevents Relation#merge from interfering. The current implementation was buggy when a default_scope existed on the target model, since we would basically end up doing: default_scope.merge(association_scope) If default_scope contained a where(foo: 'a') and association_scope contained a where(foo: 'b').where(foo: 'c') then the merger would see that the same column is representated on both sides of the merge and collapse the wheres to all but the last: where(foo: 'c') Now, the RHS of the merge is left alone. Fixes #8990
* Added test case to prevent regression of chained, preloaded scopes. (#7490)Chris Geihsler2013-01-191-0/+7
|
* deprecate `assert_blank` and `assert_present`.Yves Senn2013-01-051-2/+2
| | | | | They don't add any benefits over `assert object.blank?` and `assert object.present?`
* Revert "[#8743] Test case"Rafael Mendonça França2013-01-051-5/+0
| | | | | | This reverts commit 162e2859813b31c50700b453aa61992dfa57139e. This commit was added by mistake, sorry :bow:
* [#8743] Test caseCédric FABIANSKI2013-01-051-0/+5
| | | | | | | - Reverting #a3cf03ef99 fixes the issue Conflicts: activerecord/test/cases/relations_test.rb
* Use separate Relation subclasses for each AR classJon Leighton2012-11-301-0/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At present, ActiveRecord::Delegation compiles delegation methods on a global basis. The compiled methods apply to all subsequent Relation instances. This creates several problems: 1) After Post.all.recent has been called, User.all.respond_to?(:recent) will be true, even if User.all.recent will actually raise an error due to no User.recent method existing. (See #8080.) 2) Depending on the AR class, the delegation should do different things. For example, if a Post.zip method exists, then Post.all.zip should call it. But this will then result in User.zip being called by a subsequent User.all.zip, even if User.zip does not exist, when in fact User.all.zip should call User.all.to_a.zip. (There are various variants of this problem.) We are creating these compiled delegations in order to avoid method missing and to avoid repeating logic on each invocation. One way of handling these issues is to add additional checks in various places to ensure we're doing the "right thing". However, this makes the compiled methods signficantly slower. In which case, there's almost no point in avoiding method_missing at all. (See #8127 for a proposed solution which takes this approach.) This is an alternative approach which involves creating a subclass of ActiveRecord::Relation for each AR class represented. So, with this patch, Post.all.class != User.all.class. This means that the delegations are compiled for and only apply to a single AR class. A compiled method for Post.all will not be invoked from User.all. This solves the above issues without incurring significant performance penalties. It's designed to be relatively seamless, however the downside is a bit of complexity and potentially confusion for a user who thinks that Post.all and User.all should be instances of the same class. Benchmark --------- require 'active_record' require 'benchmark/ips' class Post < ActiveRecord::Base establish_connection adapter: 'sqlite3', database: ':memory:' connection.create_table :posts def self.omg :omg end end relation = Post.all Benchmark.ips do |r| r.report('delegation') { relation.omg } r.report('constructing') { Post.all } end Before ------ Calculating ------------------------------------- delegation 4392 i/100ms constructing 4780 i/100ms ------------------------------------------------- delegation 144235.9 (±27.7%) i/s - 663192 in 5.038075s constructing 182015.5 (±21.2%) i/s - 850840 in 5.005364s After ----- Calculating ------------------------------------- delegation 6677 i/100ms constructing 6260 i/100ms ------------------------------------------------- delegation 166828.2 (±34.2%) i/s - 754501 in 5.001430s constructing 116575.5 (±18.6%) i/s - 563400 in 5.036690s Comments -------- Bear in mind that the standard deviations in the above are huge, so we can't compare the numbers too directly. However, we can conclude that Relation construction has become a little slower (as we'd expect), but not by a huge huge amount, and we can still construct a large number of Relations quite quickly.
* Ensure ordering to make the test pass with postgresqlCarlos Antonio da Silva2012-11-181-4/+5
|
* Add test to ensure preloading works as expected with "select" and "includes".Dieter Komendera2012-11-181-0/+13
| | | | This didn't work in rails 3.1. See #2303 for more information and original pull request.
* loaded relations cannot be mutated by extending!Juanjo Bazán2012-10-281-0/+9
|
* nodoc the first_or_create methods and document alternativesJon Leighton2012-10-191-0/+10
|
* Add Relation#find_or_create_by and friendsJon Leighton2012-10-191-0/+23
| | | | | | | This is similar to #first_or_create, but slightly different and a nicer API. See the CHANGELOG/docs in the commit. Fixes #7853
* learn ActiveRecord::QueryMethods#order work with hash argumentsTima Maslyuchenko2012-10-121-0/+16
|
* remove duplicated require statements in AR test casesYves Senn2012-10-091-1/+0
|
* Model.select takes a variable list of arguments.Isaac Sanders2012-08-231-0/+8
| | | | | | This is a cleaner version of #6916. Closes #3165.
* Fix "last equality wins" logic in relation mergeErnie Miller2012-08-191-4/+7
| | | | | | This is a real fix (as compared to the band-aid in b127d86c), which uses the recently-added equality methods for ARel nodes. It has the side benefit of simplifying the merge code a bit.
* Fix merge error when Equality LHS is non-attributeErnie Miller2012-08-171-0/+16
| | | | | | | | | This is at best a band-aid for a more proper fix, since it won't truly handle the removal of the previous equality condition of these other nodes. I'm planning to put in some work on ARel toward supporting that goal. Related: rails/arel#130, ernie/squeel#153, ernie/squeel#156
* Merge pull request #7133 from roshats/fix_update_all_with_blank_argumentCarlos Antonio da Silva2012-08-151-0/+4
|\ | | | | | | Change Relation#update_all with blank argument to raise an ArgumentError instead of trying an update with empty fields.
| * raise ArgumentError if list of attributes to change is empty in update_allRoman Shatsov2012-08-141-0/+4
| |
* | Remove ActiveRecord::Base.to_aJon Leighton2012-08-031-1/+1
| | | | | | | | | | On reflection, it seems like a bit of a weird method to have on ActiveRecord::Base, and it shouldn't be needed most of the time anyway.
* | Add `Relation#load`Jon Leighton2012-08-011-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | This method explicitly loads the records and then returns `self`. Rather than deciding between "do I want an array or a relation?", most people are actually asking themselves "do I want to eager load or lazy load?" Therefore, this method provides a way to explicitly eager-load without having to switch from a `Relation` to an array. Example: @posts = Post.where(published: true).load
* | AR::Relation#order: make new order prepend old oneBogdan Gusiev2012-07-311-5/+5
| | | | | | | | | | | | | | User.order("name asc").order("created_at desc") # SELECT * FROM users ORDER BY created_at desc, name asc This also affects order defined in `default_scope` or any kind of associations.
* | Deprecate ActiveRecord::Base.scoped.Jon Leighton2012-07-271-58/+58
| | | | | | | | | | | | | | It doesn't serve much purpose now that ActiveRecord::Base.all returns a Relation. The code is moved to active_record_deprecated_finders.
* | ActiveRecord::Base.all returns a Relation.Jon Leighton2012-07-271-24/+24
|/ | | | | | | | | | | Previously it returned an Array. If you want an array, call e.g. `Post.to_a` rather than `Post.all`. This is more explicit. In most cases this should not break existing code, since Relations use method_missing to delegate unknown methods to #to_a anyway.
* support relations created with a table aliasJon Leighton2012-07-131-0/+10
|
* Relation#inspect handles doesn't perform a new query on an already-loaded ↵Jon Leighton2012-07-071-1/+12
| | | | relation
* Limit the number of records in Relation#inspectDamien Mathieu2012-07-061-0/+5
| | | | While it's interesting to have the results array, it can make a console or a webpage freeze if there are a lot of them. So this limits the number of records displayed in #inspect to 10 and tells how much were effectively found.
* Show the records in Relation#inspectJon Leighton2012-07-061-0/+5
| | | | | | | | | | | | | | | | | The reason for removing the previous implementation of `#inspect` was that it hid from you that you were dealing with a `Relation` rather than an `Array`. But it is still useful to be able to see the records, particularly if you're writing something like the following in tests: assert_equal [foo], Post.where(:bar) If the assertion fails, you want to see what records were actually loaded. So this implementation makes it clear that you've got a `Relation`, but also shows your records.
* disallow mutating a relation once loadedJon Leighton2012-06-221-0/+27
|
* a test case that ensures AR::Relation#merge can merge associationsAkira Matsuda2012-06-031-0/+8
|
* modulize AR::NullRelationAkira Matsuda2012-06-011-1/+6
| | | | now we can invoke previously added scope extension methods
* fixed typo in word finidingIvan Kukobko2012-05-201-1/+1
|
* quarantine more deprecated stuffJon Leighton2012-05-181-9/+0
|
* quarantine deprecated testsJon Leighton2012-05-181-43/+3
|
* Relation#from to accept other Relation objectsRadoslav Stankov2012-05-171-0/+7
| | | | Record.from("(#{sub_query.to_sql})") -> Record.from(sub_query) Record.from("(#{sub_query.to_sql}) a") -> Record.from(sub_query, :a)
* Return false for exists? with new records - fixes #6199.Andrew White2012-05-101-0/+1
|
* Use `take` instead of `first` to avoid unwanted implicit ordering (fixes #6147)Marcelo Silveira2012-05-051-1/+9
|
* delete_all raise an error if a limit is provided - fixes #4979Francesco Rodriguez2012-04-301-0/+4
|
* find and replace deprecated keysJon Leighton2012-04-271-1/+1
|
* %s/find(:\(first\|last\|all\), \([^()]*\))/scoped(\2).\1/gcI amongst other ↵Jon Leighton2012-04-271-8/+3
| | | | things
* remove deprecate #all usageJon Leighton2012-04-261-4/+0
|
* remove deprecated scope stuffJon Leighton2012-04-261-13/+0
|
* remove tests for #with_scope (it's now deprecated)Jon Leighton2012-04-251-1/+0
|
* Override AR::Relation methods in NullRelation.Juanjo Bazán2012-04-111-0/+33
| | | So a NullRelation (Relation#none) is chainable with database methods.
* Add Relation#find_by and Relation#find_by!Jon Leighton2012-03-301-0/+34
|
* Fix typo in ActiveRecord::Relation#blank? testRuben Davila2012-03-171-1/+1
|
* Fix ActiveRecord::Relation#blank? testsRafael Mendonça França2012-03-161-12/+15
|
* test relation presence fixThiago Almeida2012-03-161-4/+4
|