aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
Commit message (Collapse)AuthorAgeFilesLines
* Use `_relation_for_itself` wherever possibleSean Griffin2017-07-261-3/+2
| | | | | | | Anywhere that we're doing `self.class.unscoped.where(primary_key => id)` is somewhere that someone may want to extend. Even if this method isn't public API yet, this will make it easier for us to eventually expose an API around this. Plus, duplicated code makes me sad.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* `Persistence#delete` should not be affected by scopingRyuta Kamizono2017-07-181-1/+5
| | | | | `self.class.delete` is delegated to `all` and `all` is affected by scoping. It should use `unscoped` to not be affected by that.
* Merge pull request #29495 from eugeneius/_write_attributeMatthew Draper2017-07-091-1/+1
|\ | | | | Improve the performance of writing attributes
| * Rename raw_write_attribute to write_attribute_without_type_castEugene Kenny2017-07-071-1/+1
| | | | | | | | | | | | | | | | This name more accurately describes what the method does, and also disambiguates it from `_write_attribute`, which ignores aliases. We can also make the method private, since it's not public API and only called from one place - `update_columns` - without an explicit receiver.
* | 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
| |
* | Fix ActiveRecord::Persistence#touch with lockingbogdanvlviv2017-06-211-1/+1
|/ | | | | | `ActiveRecord::Persistence#touch` does not work well when optimistic locking enabled and `locking_column`, without default value, is null in the database.
* Prevent double firing the before save callback of new object when the parent ↵Ryuta Kamizono2017-04-211-6/+20
| | | | | | | | | | | | | | | | | | association saved in the callback Related #18155, #26661, 268a5bb, #27434, #27442, and #28599. Originally #18155 was introduced for preventing double insertion caused by the after save callback. But it was caused the before save issue (#26661). 268a5bb fixed #26661, but it was caused the performance regression (#27434). #27442 added new record to `target` before calling callbacks for fixing #27434. But it was caused double firing before save callback (#28599). We cannot add new object to `target` before saving the object. This is improving #18155 to only track callbacks after `save`. Fixes #28599.
* Docs: Specify return value of `save!`Jared Beck2017-01-301-0/+2
| | | | | I can never remember if it returns `self` or `true` (seems to be `true`) [ci skip]
* Add the touch option to ActiveRecord#increment! and decrement!akihiro172017-01-141-6/+10
| | | | | Supports the `touch` option from update_counters. The default behavior is not to update timestamp columns.
* Merge pull request #25427 from eugeneius/update_increment_documentationKasper Timm Hansen2017-01-081-8/+8
|\ | | | | Update increment! documentation [ci skip]
| * Update increment! documentation [ci skip]Eugene Kenny2016-08-151-8/+8
| | | | | | | | | | | | | | | | | | | | | | The `increment!` and `decrement!` methods were recently reimplemented to make them safe to call from multiple connections concurrently. This changed their behaviour in a few ways. Previously they used `update_attribute`, which calls the attribute setter method, runs callbacks, and touches the record. Now they behave more like `update_column`, writing the update to the database directly and bypassing all of those steps.
* | [ci skip] Find and record the article! 🤓Kasper Timm Hansen2017-01-031-2/+2
| | | | | | | | | | | | Closes #27555. [ Ben A. Morgan & Kasper Timm Hansen ]
* | Emulate db trigger behaviour for after_commit :destroy, :updateStefan Budeanu2016-12-091-3/+11
| | | | | | | | | | | | Race conditions can occur when an ActiveRecord is destroyed twice or destroyed and updated. The callbacks should only be triggered once, similar to a SQL database trigger.
* | Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-3/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks We pretty frequently get bug reports that "dirty is broken inside of after callbacks". Intuitively they are correct. You'd expect `Model.after_save { puts changed? }; model.save` to do the same thing as `model.save; puts model.changed?`, but it does not. However, changing this goes much farther than just making the behavior more intuitive. There are a _ton_ of places inside of AR that can be drastically simplified with this change. Specifically, autosave associations, timestamps, touch, counter cache, and just about anything else in AR that works with callbacks have code to try to avoid "double save" bugs which we will be able to flat out remove with this change. We introduce two new sets of methods, both with names that are meant to be more explicit than dirty. The first set maintains the old behavior, and their names are meant to center that they are about changes that occurred during the save that just happened. They are equivalent to `previous_changes` when called outside of after callbacks, or once the deprecation cycle moves. The second set is the new behavior. Their names imply that they are talking about changes from the database representation. The fact that this is what we really care about became clear when looking at `BelongsTo.touch_record` when tests were failing. I'm unsure that this set of methods should be in the public API. Outside of after callbacks, they are equivalent to the existing methods on dirty. Dirty itself is not deprecated, nor are the methods inside of it. They will only emit the warning when called inside of after callbacks. The scope of this breakage is pretty large, but the migration path is simple. Given how much this can improve our codebase, and considering that it makes our API more intuitive, I think it's worth doing.
* | Fix code formatting in `#save` RDoc [ci skip]Adam Crownoble2016-09-251-2/+2
| | | | | | Noticed that the `validate: false` option for `ActiveRecord::Persistence#save` and `#save!` were not formatted as code like the other examples in the documentation.
* | Return true if attribute is not changed for update_attributePrathamesh Sonpatki2016-09-231-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | - If the attribute is not changed, then update_attribute does not run SQL query, this effectively means that no change was made to the attribute. - This change was made in https://github.com/rails/rails/commit/0fcd4cf5 to avoid a SQL call. - But the change resulted into `nil` being returned when there was no change in the attribute value. - This commit corrects the behavior to return true if there is no change in attribute value. This is same as previous behavior of Rails 4.2 plus benefit of no additional SQL call. - Fixes #26593.
* | Clear attribute changes after handling lockingJakob Skjerning2016-09-141-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this the changes to the lock version column will stick around even after `touch` returns. Before: model.touch model.changes # => {"lock_version"=>[0, "1"]} After: model.touch model.changes # {}
* | Ensure that inverse associations are set before running callbacksSean Griffin2016-08-311-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
* | Refactor remove duplication.Santosh Wadghule2016-08-311-2/+6
|/
* 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.
* Use `squish` rather than `strip_heredoc`Ryuta Kamizono2016-07-041-2/+2
|
* Improve error message when record touch fails.Ben Standefer2016-07-021-1/+6
| | | | | | The current error message only indicates that a touch can fail because the record is new. In practice, we saw cases where touches were failing because the record had been destroyed. `persisted?` checks `new_record?` *and* `destroyed?`. It was confusing to get a message about a new record when in reality we were destroying records. I also included a helpful tip for users to consider using `persisted?`, `new_record?`, or `destroyed?` before touching.
* Fix a small template misrender in ActiveRecord::PersistenceGenadi Samokovarov2016-03-021-1/+1
| | | | | | | | See the end of [this] page. [ci skip] [this]: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-create
* - Updated persistence documentation to make it clear that save and save! ↵Vipul A M2016-01-311-3/+3
| | | | | | | | | | | won't update a record if validation fails. - Also fixed `update` method's documention to be uniform about this statement. Fixes #20821 [ci skip] [Vipul A M & pseidemann ]
* Merge pull request #21791 from sonalkr132/persistence-docArthur Nogueira Neves2016-01-271-8/+9
|\ | | | | Improvement in ActiveRecord::Persistence doc [ci skip]
| * Improvement in ActiveRecord::Persistence doc [ci skip]Aditya Prakash2015-10-171-8/+9
| |
* | `ActiveRecord::Base#becomes` should copy the errorsVokhmin Alexey V2015-12-141-1/+1
| |
* | Make sure we touch all the parents when touch_later.Arthur Neves2015-12-061-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The problem was that when saving an object, we would call touch_later on the parent which wont be saved immediteally, and it wont call any callbacks. That was working one level up because we were calling touch, during the touch_later commit phase. However that still didnt solve the problem when you have a 3+ levels of parents to be touched, as calling touch would affect the parent, but it would be too late to run callbacks on its grand-parent. The solution for this, is instead, call touch_later upwards when the first touch_later is called. So we make sure all the timestamps are updated without relying on callbacks. This also removed the hard dependency BelongsTo builder had with the TouchLater module. So we can still have the old behaviour if TouchLater module is not included. [fixes 5f5e6d924973003c105feb711cefdb726f312768] [related #19324]
* | Clean up and correct documentation for update_columns and update_all [ci skip]James Wen2015-12-051-0/+1
| |
* | Merge pull request #22362 from radar/toggle-documentationClaudio B2015-11-201-0/+8
|\ \ | |/ |/| Add example for AR::Persistence#toggle
| * Add example for AR::Persistence#toggleRyan Bigg2015-11-211-0/+8
| |
* | applies new doc guidelines to Active Record.Yves Senn2015-10-141-19/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | Merge pull request #11410 from bogdan/increment-concurencyJeremy Daer2015-10-101-5/+7
|\ \ | | | | | | | | | Make AR#increment! and #decrement! concurrency-safe
| * | Make #increment! and #decrement! methods concurency safeBogdan Gusiev2015-10-051-5/+7
| | |
* | | Build the `AttributeMutationTracker` lazilySean Griffin2015-10-021-1/+1
|/ / | | | | | | | | For reads, we never need to construct this object. The double `defined?` check is to avoid errors in tests
* | Encapsulate a lot of the logic from `Dirty` in an objectSean Griffin2015-09-241-1/+1
| | | | | | | | | | | | | | | | | | In order to improve the performance of dirty checking, we're going to need to duplicate all of the `previous_` methods in Active Model. However, these methods are basically the same as their non-previous counterparts, but comparing `@original_attributes` to `@previous_original_attributes` instead of `@attributes` and `@original_attributes`. This will help reduce that duplication.
* | Clean up the implementation of AR::DirtySean Griffin2015-09-241-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | This moves a bit more of the logic required for dirty checking into the attribute objects. I had hoped to remove the `with_value_from_database` stuff, but unfortunately just calling `dup` on the attribute objects isn't enough, since the values might contain deeply nested data structures. I think this can be cleaned up further. This makes most dirty checking become lazy, and reduces the number of object allocations and amount of CPU time when assigning a value. This opens the door (but doesn't quite finish) to improving the performance of writes to a place comparable to 4.1
* | `destroy` shouldn't raise when child associations fail to saveSean Griffin2015-07-241-1/+8
|/ | | | | | | | | | | | | | | | | | | | | | | | | | Deep down in the association internals, we're calling `destroy!` rather than `destroy` when handling things like `dependent` or autosave association callbacks. Unfortunately, due to the structure of the code (e.g. it uses callbacks for everything), it's nearly impossible to pass whether to call `destroy` or `destroy!` down to where we actually need it. As such, we have to do some legwork to handle this. Since the callbacks are what actually raise the exception, we need to rescue it in `ActiveRecord::Callbacks`, rather than `ActiveRecord::Persistence` where it matters. (As an aside, if this code wasn't so callback heavy, it would handling this would likely be as simple as changing `destroy` to call `destroy!` instead of the other way around). Since we don't want to lose the exception when `destroy!` is called (in particular, we don't want the value of the `record` field to change to the parent class), we have to do some additional legwork to hold onto it where we can use it. Again, all of this is ugly and there is definitely a better way to do this. However, barring a much more significant re-architecting for what I consider to be a reletively minor improvement, I'm willing to take this small hit to the flow of this code (begrudgingly).
* refactor ActiveRecord's #become by simplifying codeDiego Carrion2015-06-101-2/+1
|
* Merge pull request #19886 from henders/henders/reload_wipe_query_cacheRafael Mendonça França2015-05-271-1/+3
|\ | | | | | | Cause ActiveRecord::Base::reload to also ignore the QueryCache.
| * Cause ActiveRecord::Base::reload to also ignore the QueryCache.Shane Hender2015-04-281-1/+3
| |
* | AR::RecordNotSaved & RecordNotDestroyed should include an error messageYuki Nishijima2015-05-011-2/+2
| | | | | | | | | | | | | | When `AR::Base.save!` or `AR::Base.destroy!` is called and an exception is raised, the exception doesn't have any error message or has a weird message like `#<FailedBulb:0x0000000907b4b8>`. Give a better message so we can easily understand why it's failing to save/destroy.
* | remove trailing whitespace. [ci skip]Yves Senn2015-04-271-1/+1
| |
* | Merge pull request #19918 from vngrs/becomes_missing_note_about_stiYves Senn2015-04-271-1/+3
|\ \ | |/ |/| | | Add note about sti column value to becomes method [ci skip]
| * Add note about sti column value to becomes method [ci skip]Mehmet Emin İNAÇ2015-04-271-2/+4
|/
* Use _read_attribute(primary_key) instead of idRafael Mendonça França2015-04-191-1/+1
| | | | | | | | | This will avoid the indirection of having calling id since we already know which is the primary key column. Also this will make explicit the behavior since it is not clear that id gets the right primary key value and not just the value of the "id" column.
* Raise StaleObjectError if touched object is stale and locking is enabledMehmet Emin İNAÇ2015-04-191-3/+15
| | | | | | | | | | Fixes #19776 change test variable names and use more verbose on method less verbose use _read_attribute instead of send
* Batch touch parent recordsArthur Neves2015-04-081-1/+2
| | | | | | | | | | [fixes #18606] Make belongs_to use touch over touch_later when running the callbacks. Add more tests and small method rename Thanks Jeremy for the feedback.