aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Use separate Relation subclasses for each AR classJon Leighton2012-11-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Remove the #sum method from CollectionAssociationCarlos Antonio da Silva2012-11-211-9/+0
| | | | | | | Since edd94cee9af1688dd036fc58fd405adb30a5e0da, CollectionProxy delegates all calculation methods - except count - to the scope, which does basically what this method was doing, but since we're delegating from the proxy, the association method was never called.
* Nullify the relation at a more general level.Jon Leighton2012-11-091-2/+10
| | | | | | | | | | | | | | | 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.)
* Fix issue with collection associations and first(n)/last(n)Carlos Antonio da Silva2012-11-011-1/+3
| | | | | | | | | | | | | | | | | | | | | | | When calling first(n) or last(n) in a collection, Active Record was improperly trying to set the inverse of instance in case that option existed. This change was introduced by fdf4eae506fa9895e831f569bed3c4aa6a999a22. In such cases we don't need to do that "manually", since the way collection will be loaded will already handle that, so we just skip setting the inverse association when any argument is given to first(n)/last(n). The test included ensures that these scenarios will have the inverse of instance set properly. Fixes #8087, Closes #8094. Squashed cherry-pick from d37d40b and c368b66. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/collection_association.rb
* Fix has_many assocation w/select load after createErnie Miller2012-10-051-1/+1
| | | | | | | | | | | | | | If you create a new record via a collection association proxy that has not loaded its target, and which selects additional attributes through the association, then when the proxy loads its target, it will inadvertently trigger an ActiveModel::MissingAttributeError during attribute writing when CollectionAssociation#merge_target_lists attempts to do its thing, since the newly loaded records will possess attributes the created record does not. This error also raises a bogus/confusing deprecation warning when accessing the association in Rails 3.2.x, so cherry-pick would be appreciated!
* Count returns 0 without querying if parent is not savedFrancesco Rodriguez2012-10-031-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | Patches `CollectionAssociation#count` to return 0 without querying if the parent record is new. Consider the following code: class Account has_many :dossiers end class Dossier belongs_to :account end a = Account.new a.dossiers.build # before patch a.dossiers.count # SELECT COUNT(*) FROM "dossiers" WHERE "dossiers"."account_id" IS NULL # => 0 # after a.dosiers.count # fires without sql query # => 0 Fixes #1856.
* Remove mass_assignment_options from ActiveRecordGuillermo Iguaran2012-09-161-10/+10
|
* Remove debug code :bomb:Rafael Mendonça França2012-09-161-1/+0
|
* Don't preserve SELECT columns on COUNTSteve Klabnik2012-09-161-1/+2
| | | | | | | | | | | | | | | | | | The COUNT clause of a finder_sql relationship is being rewritten from COUNT(*) to COUNT(table_name.*). This does not appear to be valid syntax in MySQL: ``` mysql> SELECT COUNT( table_name.* ) FROM `table_name`; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) FROM `table_name`' at line 1 ``` This fixes the bug, as well as adding tests so we don't re-introduce it in the future. Fixes #3956.
* Use inversed parent for first and last child of has_many associationbrainopia2012-08-181-1/+1
|
* Changing AR:CollectionAssociation#empty? to use #exists?beerlington2012-08-051-3/+11
| | | | | COUNT(*) queries can be slow in PostgreSQL, #exists? avoids this by selecting a single record.
* s/scoped/scope/Jon Leighton2012-08-011-10/+10
|
* Deprecate :finder_sql, :counter_sql, :insert_sql, :delete_sql.Jon Leighton2012-08-011-1/+1
|
* Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."Jon Leighton2012-08-011-16/+69
| | | | | | | | | This reverts commit 3803fcce26b837c0117f7d278b83c366dc4ed370. Conflicts: activerecord/CHANGELOG.md It will be deprecated only in 4.0, and removed properly in 4.1.
* we don't need this argJon Leighton2012-07-201-2/+2
|
* Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql.Jon Leighton2012-07-201-69/+16
|
* Remove obsolete line.Jon Leighton2012-07-201-1/+0
| | | | | | | | This code is broken (it should say association_scope.uniq_value rather than options[:uniq]) but the tests still pass. I think it is designed to uniq-ify associations using finder_sql. However, I am about to remove that anyway.
* Represent association scope options as AR::Relations insternally.Jon Leighton2012-07-131-5/+5
|
* Set the hash value directly instead of using merge!Carlos Antonio da Silva2012-06-211-1/+1
|
* Remove unneeded code since pluck is respecting joins nowRafael Mendonça França2012-06-191-12/+1
|
* Add support for CollectionAssociation#delete by Fixnum or StringFrancesco Rodriguez2012-05-281-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | 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.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-05-191-10/+24
|\
| * move docs from CollectionAssociation to CollectionProxyFrancesco Rodriguez2012-05-181-113/+1
| |
| * remove incorrect example of CollectionAssociation#empty?Francesco Rodriguez2012-05-181-17/+0
| |
| * add docs to CollectionAssociation#empty?Francesco Rodriguez2012-05-181-3/+32
| |
| * add docs to CollectionAssociation#any?Francesco Rodriguez2012-05-181-0/+30
| |
| * add examples to CollectionAssociation#concatFrancesco Rodriguez2012-05-181-3/+14
| |
| * fix CollectionAssociation docsFrancesco Rodriguez2012-05-171-1/+1
| |
| * add example to CollectionAssociation#destroy_allFrancesco Rodriguez2012-05-171-3/+14
| |
| * add more explanation to CollectionAssociation docsFrancesco Rodriguez2012-05-171-0/+4
| |
| * add CollectionAssociation hierarchyFrancesco Rodriguez2012-05-171-2/+7
| |
| * add docs to CollectionAssociation#many?Francesco Rodriguez2012-05-171-1/+34
| |
| * fix CollectionAssociation#replace docsFrancesco Rodriguez2012-05-171-3/+3
| |
| * Add docs to CollectionAssociation#replaceFrancesco Rodriguez2012-05-171-3/+23
| |
* | Ensure that CollectionAssociation#replace returns proper targetPiotr Sarnacki2012-05-191-1/+1
| | | | | | | | | | | | | | | | The fix commited in e2a070c was returning the `new_target`, as a try to return whatever user replaced association with. The problem is, the resulting association target may be ordered differently. In such case we want to return the target that will be later used for that association.
* | no longer need #delete_all_on_destroyJon Leighton2012-05-181-7/+0
| |
* | Perf: Don't load the association for #delete_all.Jon Leighton2012-05-181-2/+12
|/ | | | Bug #6289
* Merge pull request #5453 from ↵Aaron Patterson2012-05-161-2/+6
|\ | | | | | | | | JonRowe/patch_uniq_has_and_belongs_to_many_when_already_loaded When Active Record has already loaded a unique association `.size` returns the wrong number.
| * when using a preloaded array and the uniq flag is set then return the size ↵Jon Rowe2012-03-151-2/+6
| | | | | | | | of the uniq array
* | Fix CollectionAssociation#replace to return new target (closes #6231)Piotr Sarnacki2012-05-161-0/+2
| |
* | CollectionProxy < RelationJon Leighton2012-05-111-7/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | Some refactor for association.kennyj2012-04-121-1/+1
|/ | | | | | | * Remove unused association_class method. * Remove a unnecessary assignment. * Move @updated to BelongsToAssociation that only reference this instance variable. * Reset @stale_state at the reset method. I think this place is right place.
* In AR depths use &:to_i before :uniq to process mixed arrays likes ["1", 1] ↵Alexey Vakhov2012-03-091-1/+1
| | | | correct
* ids_reader method fixed, test added to has_many associationgregolsen2012-01-311-1/+1
|
* Remove Array.wrap calls in ActiveRecordRafael Mendonça França2012-01-061-3/+1
|
* Use 1.9 waySantiago Pastorino2012-01-051-6/+1
|
* bypass preloading for ids_readerSergey Nartimov2011-12-181-2/+10
| | | | | when fetching ids for a collection, bypass preloading to avoid the unnecessary performance overhead
* Fix #3672 again (dependent: delete_all perf)Jon Leighton2011-12-141-0/+7
|
* load has_many associations keyed off a custom primary key if that key is ↵Brian Samson2011-11-251-1/+1
| | | | present but the record is unsaved
* Merge pull request #3507 from jmazzi/issue-3503Jeremy Kemper2011-11-031-2/+6
| | | | Preserve SELECT columns on the COUNT for finder_sql when possible