aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #19683 from tristang/require-option-for-counter-cacheRafael Mendonça França2015-08-131-1/+8
|\ | | | | | | Require explicit counter_cache option for has_many
| * Require explicit counter_cache option for has_manyTristan Gamilis2015-04-071-1/+8
| | | | | | | | | | | | | | | | | | | | Previously has_many associations assumed a counter_cache was to be used based on the presence of an appropriately named column. This is inconsistent, since the inverse belongs_to association will not make this assumption. See issues #19042 #8446. This commit checks for the presence of the counter_cache key in the options of either the has_many or belongs_to association as well as ensuring that the *_count column is present.
* | Deprecate and rename the keys for association restrict_dependent_destroyRoque Pinel2015-07-201-1/+8
|/ | | | | | | | | | | | | | | | | Previously `has_one` and `has_many` associations were using the `one` and `many` keys respectively. Both of these keys have special meaning in I18n (they are considered to be pluralizations) so by renaming them to `has_one` and `has_many` we make the messages more explicit and most importantly they don't clash with linguistical systems that need to validate translation keys (and their pluralizations). The `:'restrict_dependent_destroy.one'` key should be replaced with `:'restrict_dependent_destroy.has_one'`, and `:'restrict_dependent_destroy.many'` with `:'restrict_dependent_destroy.has_many'`. [Roque Pinel & Christopher Dell]
* Correct errors in counter cache updatingSean Griffin2015-02-031-1/+5
| | | | | | | | | | | The cache name should be converted to a string when given, not compared as a symbol. This edge case is already adequately covered by our tests, but was masked by another issue where we were incorrectly updating the counter cache twice. When paired with a bug where we didn't update the counter cache because we couldn't find a match with the name, this made it look like everything was working fine. Fixes #10865.
* Improve consistency of counter caches updating in memorySean Griffin2015-01-261-5/+15
| | | | | | | | | | | | | | | | | When we made sure that the counter gets updated in memory, we only did it on the has many side. The has many side only does the update if the belongs to cannot. The belongs to side was updated to update the counter cache (if it is able). This means that we need to check if the belongs_to is able to update in memory on the has_many side. We also found an inconsistency where the reflection names were used to grab the association which should update the counter cache. Since reflection names are now strings, this means it was using a different instance than the one which would have the inverse instance set. Fixes #18689 [Sean Griffin & anthonynavarre]
* Deprecate `false` as the way to halt AR callbacksclaudiob2015-01-021-1/+1
| | | | | | | | | | Before this commit, returning `false` in an ActiveRecord `before_` callback such as `before_create` would halt the callback chain. After this commit, the behavior is deprecated: will still work until the next release of Rails but will also display a deprecation warning. The preferred way to halt a callback chain is to explicitly `throw(:abort)`.
* Share foreign_key_present? implementation in _has_ associationsbrainopia2014-12-311-8/+1
|
* Improve the performance of reading attributesSean Griffin2014-11-181-1/+1
| | | | | | | We added a comparison to "id", and call to `self.class.primary_key` a *lot*. We also have performance hits from `&block` all over the place. We skip the check in a new method, in order to avoid breaking the behavior of `read_attribute`
* Don't expose these new APIs yet (added in 877ea78 / #16189)Godfrey Chan2014-08-161-1/+1
| | | | | | | WARNING: don't use them! They might change or go away between future beta/RC/ patch releases! Also added a CHANGELOG entry for this.
* Implement `_was` and `changes` for in-place mutations of AR attributesSean Griffin2014-08-161-1/+1
|
* Redefine macro checks for reflectionseileencodes2014-07-301-1/+1
| | | | | | | | | | | | | | Now that we define the macro on the reflection type we no longer need to check `macro == :what` on each type for `belongs_to?` or `has_one?` etc. These now default to false unless it's defined in the reflection class. Reuse existing belongs_to? method to check macros We don't need to do `:belongs_to == macro` anymore becasue we have a `belongs_to?` method. I didn't find this being used anywhere for `has_one?` or `collection?` since they were already fixed.
* Merge pull request #15266 from dv/use_counter_cache_for_empty_callGodfrey Chan2014-07-151-0/+8
|\ | | | | If a counter_cache exists, use it for #empty?
| * If a counter_cache exists, use it for #empty?David Verhasselt2014-06-101-0/+8
| |
* | Deprecate automatic counter caches on has_many :throughSean Griffin2014-06-261-1/+9
| | | | | | | | | | | | | | | | | | | | | | Reliant on https://github.com/rails/rails/pull/15747 but pulled to a separate PR to reduce noise. `has_many :through` associations have the undocumented behavior of automatically detecting counter caches. However, the way in which it does so is inconsistent with counter caches everywhere else, and doesn't actually work consistently. As with normal `has_many` associations, the user should specify the counter cache on the `belongs_to`, if they'd like it updated.
* | Always update counter caches in memory when adding recordsSean Griffin2014-06-161-0/+26
|/ | | | | | | | | | | Before, calling `size` would only work if it skipped the cache, and would return a different result from the cache, but only if: - The association was previously loaded - Or you called size previously - But only if the size was 0 when you called it This ensures that the counter is appropriately updated in memory.
* Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-1/+2
| | | | | | | | | Fix habtm reflection Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb activerecord/lib/active_record/reflection.rb activerecord/test/cases/reflection_test.rb
* remove count vareileencodes2014-05-131-4/+2
| | | | this change was unneccsary as nothing was gained from it
* rename delete_all_records to delete_or_nullify_all_recordseileencodes2014-05-131-1/+1
| | | | | | | Rename delete_all_records because this name better describes what the method is doing. We can then remove :all from the hm:t version and pull out the unoptimized call to load_target in delete_records and pass it directly.
* remove need for :all symboleileencodes2014-05-131-13/+6
| | | | | | Refactor delete_count method to only handle delete_all or nullify/nil cases and not destroy and switch to if/else rather than case statement. This refactoring allows removal of :all symbol usage.
* begin refactoring delete_records methodeileencodes2014-05-131-12/+25
| | | | | | | | | | | | | | Refactor by creating two methods delete_all_records and delete_records to be called by delete_all and delete (or destroy) respectively. This reduces the number of conditionals required to handle _how_ records get deleted. The new delete_count method handles how scope is applied to which delete action. A delete_all_records method also has to be called in has_many_through association because of how the methods are chained. This will be refactored later on.
* read_attribute is public, so we should just call itAaron Patterson2014-04-131-1/+1
|
* make tests pass on Ruby 2.2Aaron Patterson2014-03-131-4/+4
| | | | | | Apparently we've been using a buggy feature for the past 6 years: https://bugs.ruby-lang.org/issues/9593
* fix HABTM w/out primary key errors on destructionKevin Casey2014-02-021-1/+1
|
* Raise `RecordNotDestroyed` when children can't be replacedBrian Thomas Storti2013-11-251-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #12812 Raise `ActiveRecord::RecordNotDestroyed` when a child marked with `dependent: destroy` can't be destroyed. The following code: ```ruby class Post < ActiveRecord::Base has_many :comments, dependent: :destroy end class Comment < ActiveRecord::Base before_destroy do return false end end post = Post.create!(comments: [Comment.create!]) post.comments = [Comment.create!] ```` would result in a `post` with two `comments`. With this commit, the same code would raise a `RecordNotDestroyed` exception, keeping the `post` with the same `comment`.
* Make sure inverse_of is visible on the has_many callbacksArthur Neves2013-09-251-0/+1
|
* hm:t join tables may not have a primary keyAaron Patterson2013-09-061-1/+5
|
* Revert "Merge pull request #11416 from tigrish/master"Yves Senn2013-07-221-1/+1
| | | | | This reverts commit 9dc8aef084fc5ae7e3a396dd098d89da93d06fda, reversing changes made to 02e8dae6279ea25312293a3eca777faf35139c4c.
* Remove ambiguity with pluralizations and I18n keys used for association ↵Christopher Dell2013-07-131-1/+1
| | | | restrict_dependent_destroy errors
* Dropped deprecated option `:restrict` for `:dependent` in associationsNeeraj Singh2013-07-031-1/+1
|
* Removed support for deprecated `finder_sql` in associations.Neeraj Singh2013-07-021-2/+0
|
* Removed support for deprecated `counter_sql`Neeraj Singh2013-07-021-1/+1
|
* Fix the `:primary_key` option for `has_many` associations.Yves Senn2013-05-231-2/+1
| | | | | | | | | | | | | | | | | When removing records from a `has_many` association it used the `primary_key` defined on the association. Our test suite didn't fail because on all occurences of `:primary_key`, the specified column was available in both tables. This prevented the code from raising an exception but it still behaved badly. I added a test-case to prevent regressions that failed with: ``` 1) Error: HasManyAssociationsTest#test_has_many_assignment_with_custom_primary_key: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: essays.first_name: UPDATE "essays" SET "writer_id" = NULL WHERE "essays"."writer_id" = ? AND "essays"."first_name" IS NULL ```
* Update other counter caches on destroyIan Young2013-03-201-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 .
* :counter_cache option for to support custom named counter caches. Closes #7993Yves Senn2012-11-041-1/+1
|
* move dependency logic out of generated methodsJon Leighton2012-08-101-0/+22
|
* s/scoped/scope/Jon Leighton2012-08-011-3/+3
|
* Revert "Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql."Jon Leighton2012-08-011-1/+7
| | | | | | | | | This reverts commit 3803fcce26b837c0117f7d278b83c366dc4ed370. Conflicts: activerecord/CHANGELOG.md It will be deprecated only in 4.0, and removed properly in 4.1.
* Remove :finder_sql, :counter_sql, :insert_sql, :delete_sql.Jon Leighton2012-07-201-7/+1
|
* Represent association scope options as AR::Relations insternally.Jon Leighton2012-07-131-1/+1
|
* Perf: Don't load the association for #delete_all.Jon Leighton2012-05-181-2/+6
| | | | Bug #6289
* Fix #3672 again (dependent: delete_all perf)Jon Leighton2011-12-141-7/+3
|
* load has_many associations keyed off a custom primary key if that key is ↵Brian Samson2011-11-251-0/+4
| | | | present but the record is unsaved
* Perf fixJon Leighton2011-11-191-2/+6
| | | | | | | If we're deleting all records in an association, don't add a IN(..) clause to the query. Fixes #3672.
* Only save the record once when calling create! on a collection association. ↵Jon Leighton2011-05-311-2/+7
| | | | Fixes #1360.
* Remove extra white spaces on ActiveRecord docs.Sebastian Martinez2011-05-231-1/+1
|
* Move the code which builds a scope for through associations into a generic ↵Jon Leighton2011-03-101-2/+0
| | | | AssociationScope class which is capable of building a scope for any association.
* Use proper objects to do the work to build the associations (adding methods, ↵Jon Leighton2011-02-211-0/+1
| | | | callbacks etc) rather than calling a whole bunch of methods with rather long names.
* Delegate Association#options to the reflection, and replace ↵Jon Leighton2011-02-211-2/+2
| | | | 'reflection.options' with 'options'. Also add through_options and source_options methods for through associations.
* Associations - where possible, call attributes methods rather than directly ↵Jon Leighton2011-02-211-16/+16
| | | | accessing the instance variables