aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Avoid duplicated conditionalsCarlos Antonio da Silva2014-03-141-2/+1
|
* passing an ActiveRecord object to `exists?` is deprecated.Aaron Patterson2014-03-131-1/+1
| | | | | Pass the id of the object to the method by calling `.id` on the AR object.
* Remove unnecessary db call when replacing.Arthur Neves2014-02-281-1/+3
| | | | | | | When replacing a has_many association with the same one, there is no need to do a round-trip to the db to create/and drop a new transaction. [fixes #14220]
* let `insert_record` actuall save the object.Aaron Patterson2014-02-251-2/+2
| | | | | | | | | | `before_add` callbacks are fired before the record is saved on `has_and_belongs_to_many` assocations *and* on `has_many :through` associations. Before this change, `before_add` callbacks would be fired before the record was saved on `has_and_belongs_to_many` associations, but *not* on `has_many :through` associations. Fixes #14144
* Revert "speed up the collection proxy reader method, but slow down the ↵Arthur Neves2014-02-201-5/+1
| | | | | | | | constructor" This reverts commit f9e4c3c7c0c4152b62fe9202a9d12262884bb118. [fixes #14116]
* speed up the collection proxy reader method, but slow down the constructorAaron Patterson2014-02-131-1/+5
|
* Ensure AR #second, #third, etc. finders work through associationsJason Meller2014-01-211-5/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes two regressions introduced in cafe31a078 where newly created finder methods #second, #third, #forth, and #fifth caused a NoMethodError error on reload associations and where we were pulling the wrong element out of cached associations. Examples: some_book.authors.reload.second # Before # => NoMethodError: undefined method 'first' for nil:NilClass # After # => #<Author id: 2, name: "Sally Second", ...> some_book.first.authors.first some_book.first.authors.second # Before # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 1, name: "Freddy First", ...> # After # => #<Author id: 1, name: "Freddy First", ...> # => #<Author id: 2, name: "Sally Second", ...> Fixes #13783.
* Merge pull request #10134 from ↵Rafael Mendonça França2014-01-031-2/+2
|\ | | | | | | | | derikson/collection_proxy_select_with_multiple_args Change CollectionProxy#select to take the same arguments as ActiveRecord::select
| * Changed ActiveRecord::Associations::CollectionProxy#select to take multiple ↵Dan Erikson2013-04-081-2/+2
| | | | | | | | | | | | arguments. This makes the arguments the same as ActiveRecord::QueryMethods::select.
* | Merge pull request #13417 from TalkativeTree/comments_changeRichard Schneeman2013-12-191-1/+1
|\ \ | | | | | | fix CollectionAssociation's comments
| * | change CollectionAssociation's comments to say Association instead of ↵Ben Angel2013-12-191-1/+1
| | | | | | | | | | | | AssociationProxy to match changes for 3.1 removing Association proxy.
* | | Mark the arguments needed by activerecord-deprecated_finders with a TODORafael Mendonça França2013-12-111-0/+2
| | |
* | | Revert "Merge pull request #12518 from vipulnsward/remove_count_options"Rafael Mendonça França2013-12-111-1/+3
|/ / | | | | | | | | | | | | It is needed for activerecord-depecated_finders This reverts commit dcff027a5242b20c0c90eb062dddb22ccf51aed9, reversing changes made to 3a2093984ff49d86db1efeff0c7581e788ecfb9f.
* | Fix wrong behavior where associations with dependent: :destroy optionsRafael Mendonça França2013-11-011-4/+2
| | | | | | | | | | | | | | | | | | was using nullify strategy This caused a regression in applications trying to upgrade. Also if the user set the dependent option as destroy he expects to get the records removed from the database.
* | `Relation#count` doesn't use options anymore.Vipul A M2013-10-131-3/+1
| |
* | remove HABTM associationsAaron Patterson2013-10-021-1/+0
| |
* | fix .find when inverse is setArthur Neves2013-09-251-5/+4
| | | | | | | | .find([1]) should return an Array of entries, even when a invese object is in memory already
* | Merge pull request #10500 from kennyj/fix_10450Rafael Mendonça França2013-09-221-3/+3
|\ \ | | | | | | | | | | | | | | | | | | Fixed a bug in when using has_many association with :inverse_of option and UUID primary key. Conflicts: activerecord/CHANGELOG.md
| * | Fixed a bug in when using has_many association with :inverse_of option and ↵kennyj2013-05-081-3/+3
| |/ | | | | | | UUID primary key.
* | Merge pull request #12137 from lann/fix_association_first_lastRafael Mendonça França2013-09-121-3/+1
|\ \ | | | | | | | | | | | | | | | | | | Make CollectionAssociation first/last with integer fetch with query Conflicts: activerecord/CHANGELOG.md
| * | Make CollectionAssociation first/last with integer fetch with queryLann Martin2013-09-091-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When first or last is called with an integer on an unloaded association, the entire collection is loaded. This differs surprisingly from the behavior of Relation#first/last, which translate the call into a limit query. For large collections this can make a big difference in performance. Change CollectionAssociation#fetch_first_or_last_using_find? to make this kind of call delegate to Relation.
* | | ask the association for records rather than calling `send`Aaron Patterson2013-09-101-1/+2
|/ /
* | Restore the use of `#add_to_target` for nested attribute updates on existing ↵Ben Woosley2013-08-121-4/+4
| | | | | | | | | | | | | | | | | | records, and don't bother updating the association if the update is going to be rejected anyway. This requires adding a `skip_callbacks` argument to `#add_to_target` so that we don't call the callbacks multiple times in this case, which is functionally an application of existing association data, rather than an addition of a new record to the association.
* | do is_a? tests on assignment so runtime is fasterAaron Patterson2013-07-311-8/+1
| |
* | callback should always have a valueAaron Patterson2013-07-311-1/+1
| |
* | no need to to_symAaron Patterson2013-07-311-1/+1
| |
* | add a specific factory method rather than using newAaron Patterson2013-07-231-1/+1
| |
* | fix indentationNeeraj Singh2013-07-021-14/+14
| |
* | Removed support for deprecated `finder_sql` in associations.Neeraj Singh2013-07-021-38/+5
| |
* | Removed support for deprecated `counter_sql`Neeraj Singh2013-07-021-15/+11
| |
* | Do not invoke callbacks when delete_all is calledNeeraj Singh2013-06-301-13/+29
| | | | | | | | | | | | | | | | | | | | | | Method `delete_all` should not be invoking callbacks and this feature was deprecated in Rails 4.0. This is being removed. `delete_all` will continue to honor the `:dependent` option. However if `:dependent` value is `:destroy` then the default deletion strategy for that collection will be applied. User can also force a deletion strategy by passing parameter to `delete_all`. For example you can do `@post.comments.delete_all(:nullify)`
* | do not load all child records for inverse caseNeeraj Singh2013-06-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509
* | Revert "Merge pull request #10566 from neerajdotname/10509d"Jon Leighton2013-06-191-1/+1
| | | | | | | | | | | | | | | | | | | | This reverts commit 2b817a5e89ac0e7aeb894a40ae7151a0cf3cef16, reversing changes made to 353a398bee68c5ea99d76ac7601de0a5fef6f4a5. Conflicts: activerecord/CHANGELOG.md Reason: the build broke
* | do not load all child records for inverse caseNeeraj Singh2013-06-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509
* | emphasize that callbacks are called in destroy_allNeeraj Singh2013-05-131-4/+4
|/ | | | | | | Cleaned up rdoc a bit emphasizing that callbacks are called. Also removed the stress on the fact that records are always removed. If callbacks return false then records will not be deleted.
* Updated the error message for +find+ on an inverse_of associationwangjohn2013-04-011-1/+1
| | | | | so that it is consistent with the error thrown for +find+ without an inverse_of association.
* Throwing a RecordNotFound exception when a record is scanned using thewangjohn2013-04-011-1/+13
| | | | | inverse_of option. I've also refactored the code for raising a RecordNotFound exception when searching for records with ids.
* Adding a bang to method name of raise_on_type_mismatch.wangjohn2013-03-211-3/+3
|
* Properly named variable inside blockAnupam Choudhury2013-03-201-2/+2
| | | | Closes #9824.
* Calling find() on an association with an inverse will now check to seewangjohn2013-03-191-2/+3
| | | | | if the association already holds that record in memory before checking the database for the specified ids.
* Cache the association proxy objectJon Leighton2013-03-151-1/+1
| | | | | | | | This reimplements the behaviour of Rails 3, as I couldn't see why we shouldn't cache the object, and @alindeman had a good use case for caching it: https://github.com/rails/rails/commit/c86a32d7451c5d901620ac58630460915292f88b#commitcomment-2784312
* Deprecate the `:distinct` option for `Relation#count`.Yves Senn2013-03-151-2/+3
| | | | | | | We moved more and more away from passing options to finder / calculation methods. The `:distinct` option in `#count` was one of the remaining places. Since we can now combine `Relation#distinct` with `Relation#count` the option is no longer necessary and can be deprecated.
* rename `Relation#uniq` to `Relation#distinct`. `#uniq` still works.Yves Senn2013-03-151-5/+6
| | | | | | | | The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our Relation API is close to SQL terms I renamed `#uniq` to `#distinct`. There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue to work. I also updated the documentation to promote the use of `#distinct`.
* Show warning message if delete_all is firing callbacksNeeraj Singh2013-03-111-0/+9
| | | | | | `post.comments.delete_all` will fire callbacks if :dependent option is :destroy . It will be fixed in Rails 4.1 . In the meantime display a warning . Look at #9567 for details .
* `CollectionAssociation#empty?` respects newly builded recordsYves Senn2013-01-131-1/+1
|
* 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!