aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/counter_cache.rb
Commit message (Collapse)AuthorAgeFilesLines
* Remove force parent loading when counter cache child is created/destroyedRyuta Kamizono2018-09-261-6/+2
| | | | | | | `association.increment_counters` and `association.decrement_counters` works regardless of parent target is loaded or not. Related 52e11e462f6114a4d12225c639c5f501f0ffec7a.
* Remove unused splat args in `_create_record`Ryuta Kamizono2018-09-021-2/+1
| | | | | | | | | | | | `_create_record` is passed `attribute_names` only. ``` % git grep -n '_create_record(attribute_names' lib/active_record/attribute_methods/dirty.rb:173: def _create_record(attribute_names = attribute_names_for_partial_writes) lib/active_record/counter_cache.rb:162: def _create_record(attribute_names = self.attribute_names) lib/active_record/locking/optimistic.rb:64: def _create_record(attribute_names = self.attribute_names) lib/active_record/persistence.rb:738: def _create_record(attribute_names = self.attribute_names) ```
* Extract `Relation#update_counters` for internal useRyuta Kamizono2018-07-301-21/+1
| | | | | | The target object for counter cache is not always determined by the primary key value on the model. I'd like to extract `update_couters` onto the `Relation` for the internal use.
* Fix `touch` option to behave consistently with `Persistence#touch` methodRyuta Kamizono2018-06-181-12/+10
| | | | | | | | | | | | | | | | `touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
* Avoid a subquery in updating counter cacheRyuta Kamizono2018-05-261-1/+7
| | | | Since UPDATE with a subquery doesn't work on MySQL.
* removed unnecessary returnsShuhei Kitagawa2017-10-281-1/+1
|
* Merge pull request #29765 from lugray/fix_counter_cacheRafael França2017-07-241-1/+0
|\ | | | | Fix `counter_cache` double increment
| * Add test for fixed `counter_cache` double incrementLisa Ugray2017-07-191-1/+0
| | | | | | | | | | | | | | | | | | | | | | When an `after_create` callback did `update_attributes` on a record with multiple `belongs_to` associations with counter caches, even numbered associations would have their counters double-incremented. Fixes to `ActiveModel::Dirty` in 020abad fixed this. This adds regression tests for this bug fixed incidentally in the other commit, which also removed the need for the workaround using @_after_create_counter_called.
* | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|/
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Revert "Don't guard against `touch: []`."Kasper Timm Hansen2017-01-151-1/+2
| | | | | | | | | | `timestamp_attributes_for_updates_in_model` returns an empty array when a model has no `updated_at` or `updated_on`. So my previously thought uncommon case is a lot more likely now. This reverts commit a0a1ede8c2eb6436571eae8778033162d1f9dcc3.
* Don't guard against `touch: []`.Kasper Timm Hansen2017-01-151-2/+1
| | | | | | | | | | | | | | | | | | | | Closes #27683. Seeing a code sample that leads to what we're guarding against: ```ruby Topic.update_counters(1, replies_count: 1, touch: []) ``` It doesn't look like a case people would ever intentionally end up with. Thus we're better off sparing the conditional. Note: it could happen if a method returns an empty array that's then passed to `update_counters` and its touchy friends. But `[].presence` can fix that once people see their query blow up. [ Eugene Kenny & Kasper Timm Hansen ]
* Add the touch option to ActiveRecord#increment! and decrement!akihiro172017-01-141-1/+2
| | | | | Supports the `touch` option from update_counters. The default behavior is not to update timestamp columns.
* Counter cache touching don't need object finding anymoreRyuta Kamizono2017-01-031-6/+5
| | | | | `current_time_from_proper_timezone` and timestamp attributes methods was pushed up to class method.
* Fix update counters of multiple records with touch: trueRyuta Kamizono2017-01-031-1/+1
| | | | | | | | | | | | | | | Currently does not work the following example in the doc: ```ruby # For the Posts with id of 10 and 15, increment the comment_count by 1 # and update the updated_at value for each counter. Post.update_counters [10, 15], comment_count: 1, touch: true # Executes the following SQL: # UPDATE posts # SET comment_count = COALESCE(comment_count, 0) + 1, # `updated_at` = '2016-10-13T09:59:23-05:00' # WHERE id IN (10, 15) ```
* [ci skip] Use touch; slim wording.Kasper Timm Hansen2017-01-021-2/+2
| | | | | | * Rename update -> touch to remain consistent with the other docs language of "touch"'ing. * Remove the sentence that's repeated from just above and rephrase.
* Fix grammar in active_record/counter_cache.rb [ci skip]kenta-s2017-01-021-3/+3
|
* `touch_time` should be type casted to respect the precision of the columnRyuta Kamizono2017-01-021-3/+1
|
* Prefer `each` over `map` because unused return valueRyuta Kamizono2017-01-021-1/+1
|
* Don't invoke `touch_updates` if `touch` does not suppliedRyuta Kamizono2017-01-021-3/+3
| | | | | `touch_updates` calls `Time.now` via `current_time_from_proper_timezone` so it is better to not invoke `touch_updates` if it is unnecessary.
* Fix tests with counter cache touching and more.Kasper Timm Hansen2017-01-011-18/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Refactor to use `touch_updates` Ensures we only call `current_time_from_proper_timezone` from one place. * Clarify touch default in tests. Not interested in what happens when passed false but that nothing passed means no touching. * Backdate touched columns in tests. We can't be sure a test progresses through time, so our touching code may be working correctly but the test itself is brittle. Fix by backdating that's further in the past akin to what the timestamps tests do: https://github.com/rails/rails/blob/d753645d40e925973724e4c3a8617b654da90e41/activerecord/test/cases/timestamp_test.rb#L17 * Expand changelog entry. Elaborate and show examples. Closes #26995. [ Jarred Trost & Kasper Timm Hansen ]
* Added option to ActiveRecord::CounterCache methods.Jarred Trost2017-01-011-8/+68
|
* code gardening: removes redundant selfsXavier Noria2016-08-081-1/+1
| | | | | | | | | A few have been left for aesthetic reasons, but have made a pass and removed most of them. Note that if the method `foo` returns an array, `foo << 1` is a regular push, nothing to do with assignments, so no self required.
* applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-2/+2
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Pluralize counter_cache column name in example [ci skip]Amit Thawait2016-01-201-4/+4
|
* applies new doc guidelines to Active Record.Yves Senn2015-10-141-6/+6
| | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* Improve consistency of counter caches updating in memorySean Griffin2015-01-261-1/+1
| | | | | | | | | | | | | | | | | 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]
* Go through normal `update_all` logic when updating counter cachesSean Griffin2014-12-261-4/+3
| | | | Part of a larger refactoring to remove type casting behavior from Arel
* remove useless methodsSergey Alekseev2014-12-031-10/+0
|
* No need to call to_sym hereGodfrey Chan2014-09-201-1/+1
| | | | | The hash is now string-keyed, and [_]reflect_on_association calls `to_s` on the argument anyway.
* 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.
* Add `:all` argument to `count` in `reset_counters`Cade Truitt2014-07-021-1/+1
| | | | | | | | | | Prior to this fix, if an association had a scope with a `select`, calls to `reset_counters` would generate invalid SQL and throw: ActiveRecord::StatementInvalid: [$DB_ADAPTER]: wrong number of arguments to function COUNT() References #10710, #13648
* Refactoring .reflections public method.Arthur Neves2014-05-261-1/+1
| | | | | | Now the internal reflections will hold a reference to its public representation, so when the outside world calls `Account.reflection` we can build a list of public reflections.
* Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-3/+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
* Updates reset_counters to allow counter name in paramsJason Normore2014-05-161-5/+10
| | | | | | | Add support for counter name to be passed as parameter on `CounterCache::ClassMethods#reset_counters`. This is to be consistent with the other methods in the module that all accept counter name.
* Restore the destroy_by_association check in post destroy counter cacheJean Boussier2014-04-151-2/+9
|
* Set _after_create_counter_called flag to make update counter cache workJean Boussier2014-04-151-1/+6
|
* Use inheritance chain instead of callbacks to increment counter caches after ↵Jean Boussier2014-04-141-4/+4
| | | | destroy
* Use inheritance chain instead of callbacks to increment counter caches after ↵Jean Boussier2014-04-141-0/+16
| | | | create
* Make counter cache decrementation on destroy idempotentJean Boussier2014-04-131-0/+21
|
* Spelling and Grammar checksAkshay Vishnoi2013-12-121-4/+4
|
* changed update counter to act on unscoped modelheruku2013-11-261-1/+1
|
* pass the pk to compile_updateAaron Patterson2013-11-161-1/+1
|
* update_counters accepts a hash, not an array of hashesMichael Kozono2013-05-211-1/+1
|
* `#reset_counters` verifies counter names.Yves Senn2013-03-151-1/+2
| | | | | | | Closes #9724. Raise an `ArgumentError` when the name of the counter does not match an association name.
* grammar improvements for increment_counter and decrement_counter docsMatthew Robertson2012-12-021-6/+8
|
* increment_counter and decrement_counter can accept and array of ids as an argMatthew Robertson2012-12-021-2/+2
|
* 1.9 Syntax related changesAvnerCohen2012-11-101-2/+2
|