aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
Commit message (Collapse)AuthorAgeFilesLines
* 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.
* call `sync_with_transaction_state` inside `persisted?` then check ivarsAaron Patterson2015-03-021-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | directly calling `sync_with_transaction_state` is not fast, so if we call it once, we can improve the performance of the `persisted?` method. This is important because every call to `url_for(model)` will call `persisted?`, so we want that to be fast. Here is the benchmark: ```ruby require 'active_record' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) end class Article < ActiveRecord::Base; end article = Article.new.tap(&:save!) Benchmark.ips do |x| x.report("persisted?") do article.persisted? end end ``` Before this patch: ``` $ bundle exec ruby -rbenchmark/ips persisted.rb Calculating ------------------------------------- persisted? 3.333k i/100ms ------------------------------------------------- persisted? 51.037k (± 8.2%) i/s - 253.308k ``` After: ``` $ bundle exec ruby -rbenchmark/ips persisted.rb Calculating ------------------------------------- persisted? 7.172k i/100ms ------------------------------------------------- persisted? 120.730k (± 5.1%) i/s - 602.448k ```
* Merge pull request #16989 from Empact/reload-cache-clearRafael Mendonça França2015-02-201-3/+0
|\ | | | | | | Isolate access to @associations_cache and @aggregations_cache to the Associations and Aggregations modules, respectively.
| * Isolate access to @associations_cache and @aggregations cache to the ↵Ben Woosley2014-09-281-3/+0
| | | | | | | | | | | | | | | | Associations and Aggregations modules, respectively. This includes replacing the `association_cache` accessor with a more limited `association_cached?` accessor and making `clear_association_cache` and `clear_aggregation_cache` private.
* | Merge pull request #17139 from mfazekas/fix_becomes_changed_attributesRafael Mendonça França2015-02-201-1/+2
|\ \ | | | | | | | | | Always reset changed attributes in becomes
| * | Always reset changed attributes in becomesMiklos Fazekas2015-02-041-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When ```becomes``` changes @attributes it should also change @changed_attributes. Otherwise we'll experience a kind of split head situation where attributes are coming from ```self```, but changed_attributes is coming from ```klass.new```. This affects the inheritance_colmn as it's changed by new for example. Fixes #16881
* | | Add `time` option to `#touch`Hyonjee Joo2015-02-181-6/+8
|/ / | | | | | | | | | | Fixes #18905. `#touch` now takes time as an option. Setting the option saves the record with the updated_at/on attributes set to the current time or the time specified. Updated tests and documentation accordingly.
* | add destroyed records to the currend transactionAaron Patterson2015-02-011-0/+1
| |
* | Use 'public_send' over the 'send' method for object's properties.Santosh Wadghule2015-01-231-2/+2
| |
* | Run SQL only if attribute changed for update_attribute methodPrathamesh Sonpatki2015-01-181-1/+1
| | | | | | | | | | - This is based on https://github.com/rails/rails/issues/18400 but tackling same issue with update_attribute method instead of update method.
* | Go through normal where logic in destroySean Griffin2015-01-141-9/+1
| | | | | | | | | | | | Building the Arel AST, and manipulating the relation manually like this is prone to errors and breakage as implementation details change from underneath it.
* | Deprecate `false` as the way to halt AR callbacksclaudiob2015-01-021-14/+14
| | | | | | | | | | | | | | | | | | | | 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)`.
* | Add doc for `:touch` option of AR::Base#saveclaudiob2014-12-301-0/+8
| | | | | | | | | | | | | | | | | | ActiveRecord::Base `save` and `save!` take an option boolean `:touch` parameter since #18225 (stems from #18202). This commit document that parameter. [ci skip]
* | Provide :touch option to save() to accommodate saving without updating ↵Dan Olson2014-12-271-6/+6
| | | | | | | | timestamps. [#18202]
* | Correct grammar to fix #18182 [ci skip]Zachary Scott2014-12-241-1/+1
| |
* | document that `.delete` does work on `#readonly?` records. Closes #11860Yves Senn2014-12-031-0/+2
| | | | | | | | | | | | | | | | [ci skip] This is due to the fact that `.delete` is directly translated to SQL. It tries to follow the same rules as `.delete_all` which is not able to verify that records are `#readonly?`.
* | Fix a bug where AR::RecordNotSaved loses error messagesYuki Nishijima2014-11-271-1/+1
| | | | | | | | | | | | Since 3e30c5d, it started ignoring the given error message. This commit changes the behavior of AR::RecordNotSaved#initialize so that it no longer loses the given error message.
* | Add #record attribute to RecordNotFound and RecordDestroyed exceptions.Recursive Madman2014-11-261-2/+2
| | | | | | | | This allows these exceptions to be handled generically in conjunction with RecordInvalid.
* | Remove the unused second argument to `substitute_at`Sean Griffin2014-11-171-1/+1
| | | | | | | | Oh hey, we got to remove some code because of that!
* | Revert "Improve performance of AR object instantiation"Sean Griffin2014-11-141-25/+3
| | | | | | | | | | | | | | | | | | | | This reverts commit 8fee923888192a658d8823b31e77ed0683dfd665. Conflicts: activerecord/lib/active_record/attribute_set/builder.rb This solution sucks, and is hard to actually apply across the board. Going to try other solutions
* | Print out a meaningful error when ActiveRecord::ReadOnlyRecord is raisedFranky W2014-11-061-2/+2
| | | | | | | | | | | | | | Currently, there is no messages which get printed out. Convoluted system may have hooks that create other objects in which case we only fail with no messages. This commit changes this information allowing you to know which object is the one that actually raised the error.
* | Improve performance of AR object instantiationSean Griffin2014-11-051-3/+25
|/ | | | | | | We introduced a performance hit by adding an additional iteration through a model's attributes on creation. We don't actually need the values from `Result` to be a hash, we can separate the columns and values and zip them up ourself during the iteration that we have to do.
* Implement `_was` and `changes` for in-place mutations of AR attributesSean Griffin2014-08-161-1/+1
|
* update error message to reflect that the record could have been destroyedlsylvester2014-08-111-1/+2
|
* Rephrase how we explain RecordInvalid exception in the context ofZachary Scott2014-08-071-3/+5
| | | | | | `#create!` regarding validations in contrast to the behavior of `#create`. Also describe creating multiple objects using an array of hashes as the +attributes+ parameter. [ci skip] /cc #16384