aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Adding a bang to method name of raise_on_type_mismatch.wangjohn2013-03-216-8/+8
|
* Update other counter caches on destroyIan Young2013-03-202-2/+2
|
* 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.
* 1. Change from each to each_value since we did not use keyVipul A M2013-03-191-1/+1
| | | | 2. drop assignment of value to sum in test
* If a counter_cache is defined, then using update_attributes and changingJohn Wang2013-03-151-0/+20
| | | | | the primary key on an association will make sure that the corresponding counter on the association is changed properly. Fixes #9722.
* 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-153-9/+11
| | | | | | | | 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`.
* hide more data in the schema cacheAaron Patterson2013-03-141-1/+1
|
* dependent: :destroy should call destroy_allNeeraj Singh2013-03-111-2/+3
| | | | | | | | | | | | | | Commit https://github.com/rails/rails/pull/9668 shows warning when `delete_all` is invoked with `:dependent` option `:destroy`. Unfortunately invoking `Post.destroy_all` invokes `post.comments.delete_all` as part of `has_many` callbacks. This commit ensures that instead `post.comments.destroy_all` is invoked and in the process no warning is generated. See issue #9567 for details .
* 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 .
* Deprecate #connection in favour of accessing it via the classBen Moss2013-03-091-3/+3
| | | | | This allows end-users to have a `connection` method on their models without clashing with ActiveRecord internals.
* deal with `#append` and `#prepend` on association collections.Yves Senn2013-03-011-0/+5
| | | | | | | | | | | Closes #7364. Collection associations behave similar to Arrays. However there is no way to prepend records. And to append one should use `<<`. Before this patch `#append` and `#prepend` did not add the record to the loaded association. `#append` now behaves like `<<` and `#prepend` is not defined.
* don't use non-ascii ' chars in documentationYves Senn2013-03-011-4/+4
|
* Fix touching an invalid parent record for belongs_toOlek Janiszewski2013-02-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | If the parent of a `belongs_to` record fails to be saved due to validation errors, `touch` will be called on a new record, which causes an exception (see https://github.com/rails/rails/pull/9320). Example: class Owner < ActiveRecord::Base validates_presence_of :name end class Pet < ActiveRecord::Base belongs_to :owner, touch: true end pet = Pet.new(owner: Owner.new) # Before, this line would raise ActiveRecord::ActiveRecordError # "can not touch on a new record object" pet.save
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-02-261-1/+1
|\
| * BELONGS TO ASSOCIATION | Typo fixlibin2013-02-251-1/+1
| |
* | No need to send public methodsAkira Matsuda2013-02-262-2/+2
| |
* | don't apply invalid ordering when preloading hmt associations.Yves Senn2013-02-241-2/+2
|/ | | | | | | | | | | | | | | closes #8663. When preloading a hmt association there two possible scenarios: 1.) preload with 2 queries: first hm association, then hmt with id IN () 2.) preload with join: hmt association is loaded with a join on the hm association The bug was happening in scenario 1.) with a normal order clause on the hmt association. The ordering was also applied when loading the hm association, which resulted in the error. This patch only applies the ordering the the hm-relation if we are performing a join (2). Otherwise the order will only appear in the second query (1).
* don't cache invalid subsets when preloading hmt associations.Yves Senn2013-02-141-1/+2
| | | | closes #8423.
* Reverting 16f6f25 (Change behaviour with empty array in where clause)Guillermo Iguaran2013-02-081-1/+1
|
* Change behaviour with empty array in where clauserobertomiranda2013-02-081-1/+1
|
* Refactor to use each_key, remove extra spacesCarlos Antonio da Silva2013-01-281-1/+1
|
* Prevent Relation#merge from collapsing wheres on the RHSJon Leighton2013-01-271-4/+8
| | | | | | | | | | | | | | | | | | | | | | | 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
* Undeprecate the :extend optionJon Leighton2013-01-182-1/+3
| | | | | | | Suggested by @dhh. It doesn't affect the generated SQL, so seems reasonable to continue to allow it as an association option.
* CollectionProxy should be default scopedJon Leighton2013-01-181-0/+1
| | | | Fixes #8795
* `CollectionAssociation#empty?` respects newly builded recordsYves Senn2013-01-131-1/+1
|
* Merge pull request #8568 from inossidabile/fix-in_clause_lengthJon Leighton2013-01-111-1/+1
|\ | | | | Correct source for in_clause_length for eager loading (Fix for #8474)
| * Eager loading made to use relation's in_clause_length instead of host's one ↵Boris Staal2012-12-201-1/+1
| | | | | | | | (fixes #8474)
* | Merge pull request #8823 from acapilleri/target_reflection_has_associated_recordCarlos Antonio da Silva2013-01-081-5/+1
|\ \ | | | | | | Refactor target_reflection_has_associated_record?
| * | target_reflection_has_associated_record? refactoringAngelo Capilleri2013-01-081-5/+1
| | |
* | | Merge pull request #8826 from acapilleri/different_targetCarlos Antonio da Silva2013-01-081-2/+5
|\ \ \ | | | | | | | | Improved different_target conditions
| * | | improved different_target conditionsAngelo Capilleri2013-01-081-2/+5
| |/ /
* | | Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-01-091-2/+2
|\ \ \ | |/ / |/| | | | | | | | Conflicts: guides/source/getting_started.md
| * | prefer american spelling of 'behavior'Gosha Arinich2013-01-071-2/+2
| |/
* | Rename update_attributes method to update, keep update_attributes as an aliasAmparo Luna + Guillermo Iguaran2013-01-031-1/+1
| |
* | refactoring to_sym of Symbol in preloadAngelo capilleri2013-01-021-1/+3
|/
* Fix for has_many_through counter_cache bugMatthew Robertson2012-12-141-0/+5
| | | | | | This commit fixes reported issue #7630 in which counter caches were not being updated properly when replacing has_many_through relationships
* 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-302-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 that associations have a symbol argument.Steve Klabnik2012-11-281-0/+2
|/ | | | Fixes #7418.
* Merge pull request #8291 from senny/8265_build_with_polymorphic_associationRafael Mendonça França2012-11-221-1/+2
|\ | | | | | | | | | | | | prevent mass assignment of polymorphic type when using `build` Conflicts: activerecord/CHANGELOG.md
| * prevent mass assignment of polymorphic type when using `build`Yves Senn2012-11-221-1/+2
| | | | | | | | Closes #8265
* | 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.
* Do not create useless database transaction when building `has_one` association.Bogdan Gusiev2012-11-101-1/+9
|