aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_proxy.rb
Commit message (Collapse)AuthorAgeFilesLines
* Replace comments' non-breaking spaces with spacesclaudiob2012-12-041-3/+3
| | | | | | | | | | Sometimes, on Mac OS X, programmers accidentally press Option+Space rather than just Space and don’t see the difference. The problem is that Option+Space writes a non-breaking space (0XA0) rather than a normal space (0x20). This commit removes all the non-breaking spaces inadvertently introduced in the comments of the code.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-12-011-3/+13
|\ | | | | | | | | Conflicts: guides/source/active_record_validations.md
| * copy edits [ci skip]Vijay Dev2012-12-011-1/+1
| |
| * add documentation to CollectionProxy #length and #size methods [ci skip]Francesco Rodriguez2012-11-291-2/+7
| |
| * add documentation to CollectionProxy#empty?Francesco Rodriguez2012-11-291-1/+6
| |
* | Use separate Relation subclasses for each AR classJon Leighton2012-11-301-2/+2
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* fix warningsJon Leighton2012-11-091-1/+1
|
* Delegate all calculations to the scope.Jon Leighton2012-11-091-4/+2
| | | | | | | So that the scope may be a NullRelation and return a result without executing a query. Fixes #7928
* CollectionProxy#pluck issues no query for a new_record? ownerJon Leighton2012-11-091-0/+4
| | | | | | | Fixes #8102. I couldn't find a nicer way to deal with this than delegate the call to #scope, which will be a NullRelation when we want it to be.
* Nullify the relation at a more general level.Jon Leighton2012-11-091-4/+3
| | | | | | | | | | | | | | | This allows us to avoid hacks like the "return 0 if owner.new_record?" in #count (which this commit removes). Also, the relevant foreign key may actually be present even on a new owner record, in which case we *don't* want a null relation. This logic is encapsulated in the #null_scope? method. We also need to make sure that the CollectionProxy is not 'infected' with the NullRelation module, or else the methods from there will override the definitions in CollectionProxy, leading to incorrect results. Hence the nullify: false option to CollectionAssociation#scope. (This feels a bit nasty but I can't think of a better way.)
* Relations built off collection associations with an unsaved owner should be ↵Jon Leighton2012-11-091-2/+3
| | | | | | | | | | null relations For example, the following should not run any query on the database: Post.new.comments.where(body: 'omg').to_a # => [] Fixes #5215.
* fix AR::Associations::CollectionProxy#delete broken documentation [ci skip]Francesco Rodriguez2012-09-211-23/+2
|
* update AR::Associations::CollectionProxy#loaded? documentation [ci skip]Francesco Rodriguez2012-09-211-1/+6
|
* Remove mass_assignment_options from ActiveRecordGuillermo Iguaran2012-09-161-6/+6
|
* Remove mass assignment security from ActiveRecordGuillermo Iguaran2012-09-161-1/+0
|
* Update documentation for CollectionProxyMarc-Andre Lafortune2012-09-121-8/+2
|
* s/scoped/scope/Jon Leighton2012-08-011-3/+3
|
* Add CollectionProxy#scopeJon Leighton2012-08-011-1/+5
| | | | | | | | | | | | | | | | This can be used to get a Relation from an association. Previously we had a #scoped method, but we're deprecating that for AR::Base, so it doesn't make sense to have it here. This was requested by DHH, to facilitate code like this: Project.scope.order('created_at DESC').page(current_page).tagged_with(@tag).limit(5).scoping do @topics = @project.topics.scope @todolists = @project.todolists.scope @attachments = @project.attachments.scope @documents = @project.documents.scope end
* Use explicit delegationsJon Leighton2012-08-011-119/+79
| | | | | | | This makes it easier to see what the documentation refers to. It also means that we are not doing unnecessary work for delegations that have no args / splats / block / etc.
* fix typo in collection proxyAccessd2012-07-311-1/+1
|
* Deprecate ActiveRecord::Base.scoped.Jon Leighton2012-07-271-5/+1
| | | | | | | It doesn't serve much purpose now that ActiveRecord::Base.all returns a Relation. The code is moved to active_record_deprecated_finders.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-05-301-1/+1
|\
| * change example on CollectionProxy#delete to accept multiple valuesFrancesco Rodriguez2012-05-281-1/+1
| |
* | Add support for CollectionAssociation#delete by Fixnum or StringFrancesco Rodriguez2012-05-281-0/+26
|/ | | | | | | | | | | | | | | | | | | | | | | | I found the next issue between CollectionAssociation `delete` and `destroy`. class Person < ActiveRecord::Base has_many :pets end person.pets.destroy(1) # => OK, returns the destroyed object person.pets.destroy("2") # => OK, returns the destroyed object person.pets.delete(1) # => ActiveRecord::AssociationTypeMismatch person.pets.delete("2") # => ActiveRecord::AssociationTypeMismatch Adding support for deleting with a fixnum or string like `destroy` method.
* add CollectionProxy#uniq documentationFrancesco Rodriguez2012-05-261-0/+21
|
* add :nodoc: to CollectionProxy#initializeFrancesco Rodriguez2012-05-251-1/+1
|
* add CollectionProxy#== documentationFrancesco Rodriguez2012-05-251-0/+24
|
* add CollectionProxy#count documentationFrancesco Rodriguez2012-05-251-0/+26
|
* add CollectionProxy#to_ary documentationFrancesco Rodriguez2012-05-251-2/+35
|
* add CollectionProxy#delete documentationFrancesco Rodriguez2012-05-251-1/+104
|
* copy edits in collection proxy docs [ci skip]Vijay Dev2012-05-231-25/+18
|
* add CollectionProxy#length documentationFrancesco Rodriguez2012-05-221-5/+37
|
* add CollectionProxy#size documentationFrancesco Rodriguez2012-05-221-2/+25
|
* add :call-seq: to +first+ and +last+ CollectionProxy methodsFrancesco Rodriguez2012-05-221-0/+6
|
* add CollectionProxy#create! documentationFrancesco Rodriguez2012-05-221-0/+22
|
* add CollectionProxy#create documentationFrancesco Rodriguez2012-05-221-0/+33
|
* add more examples to CollectionProxy#findFrancesco Rodriguez2012-05-221-0/+9
|
* adding :call-seq: to CollectionProxy methodsFrancesco Rodriguez2012-05-221-0/+39
|
* add CollectionProxy#build documentationFrancesco Rodriguez2012-05-221-0/+31
|
* remove repeated documentation in CollectionProxy#clearFrancesco Rodriguez2012-05-221-24/+2
|
* update CollectionProxy#clear documentationFrancesco Rodriguez2012-05-211-3/+2
|
* update CollectionProxy#delete_all documentationFrancesco Rodriguez2012-05-211-2/+29
|
* add CollectionProxy#delete_all documentationFrancesco Rodriguez2012-05-211-0/+65
|
* fix CollectionProxy documentation markupFrancesco Rodriguez2012-05-211-8/+8
|
* add CollectionProxy#reload documentationFrancesco Rodriguez2012-05-211-0/+18
|
* improve CollectionProxy#destroy documentationFrancesco Rodriguez2012-05-211-5/+37
|
* add CollectionProxy#destroy documentationFrancesco Rodriguez2012-05-211-1/+42
|
* update CollectionProxy#destroy_all documentationFrancesco Rodriguez2012-05-201-1/+9
|
* add CollectionProxy#select documentationFrancesco Rodriguez2012-05-191-2/+55
|
* add CollectionProxy#find documentationFrancesco Rodriguez2012-05-191-0/+20
|