aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Don't update counter cache unless the record is actually savedRyuta Kamizono2018-09-191-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a 4th attempt to make counter cache transactional completely. Past attempts: #9236, #14849, #23357. All existing counter cache issues (increment/decrement twice, lost increment) are caused due to updating counter cache on the outside of the record saving transaction by assigning belongs_to record, even though assigning that doesn't cause the record saving. We have the `@_after_replace_counter_called` guard condition to mitigate double increment/decrement issues, but we can't completely prevent that inconsistency as long as updating counter cache on the outside of the transaction, since saving the record is not always happened after that. We already have handling counter cache after create/update/destroy, https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/counter_cache.rb#L162-L189 https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/associations/builder/belongs_to.rb#L33-L59 so just removing assigning logic on the belongs_to association makes counter cache transactional completely. Closes #14849. Closes #23357. Closes #31493. Closes #31494. Closes #32372. Closes #33113. Closes #33117 Closes #33129. Closes #33458.
* Fix `belongs_to_counter_cache_after_update` to respect polymorphic type changeRyuta Kamizono2018-05-271-1/+4
|
* Extract all `base_class.name` as `polymorphic_name`Ryuta Kamizono2018-03-041-1/+1
| | | | | | | This is an alternative of #29722, and follow up of #32048. This does not change the current behavior, but makes it easier to modify all polymorphic names consistently.
* Bugfix foreign key replacement in inverse associationBogdan Gusiev2017-12-271-6/+1
| | | | when model is added to collection association
* Fix broken doc for Active Record [ci skip]Yoshiyuki Hirano2017-08-251-1/+1
|
* 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
|
* Fix nil assignment to polymorphic belongs_toJeremy Kemper2013-12-181-0/+5
| | | | | | | | Assigning nil to a polymorphic belongs_to would nullify its _id field by not its _type field. Fixes failing test from c141dfc838a5dca9f197814410fa5d44c143129c. Regression from 1678e959e973de32287b65c52ebc6cce87148951.
* remove nil checkAaron Patterson2013-12-121-1/+1
| | | | | calling replace_keys could possibly do a nil check twice, this commit reduces it to once.
* Adding a bang to method name of raise_on_type_mismatch.wangjohn2013-03-211-1/+1
|
* @stale_state should be nil when a model isn't saved.kennyj2012-04-131-1/+2
|
* Fix private methods which are delegated to. This previously worked because ↵Jon Leighton2011-08-151-5/+5
| | | | Module#delegate previously ignored method visibility.
* Handle polymorphic_type NOT NULL-able columns as well.thedarkone2011-05-211-1/+1
|
* Rename target_klass to klassJon Leighton2011-02-211-2/+2
|
* Associations - where possible, call attributes methods rather than directly ↵Jon Leighton2011-02-211-4/+4
| | | | accessing the instance variables
* Allow assignment on has_one :through where the owner is a new record [#5137 ↵Jon Leighton2011-01-031-13/+4
| | | | | | | | | | state:resolved] This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct. Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things. Phew, long commit message!
* Have a proper AssociationReflection#foreign_type method rather than using ↵Jon Leighton2011-01-031-3/+3
| | | | options[:foreign_type]
* Rename AssociationReflection#primary_key_name to foreign_key, since the ↵Jon Leighton2010-12-311-1/+1
| | | | options key which it relates to is :foreign_key
* Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociation to ↵Jon Leighton2010-12-311-51/+13
| | | | inherit from it
* Refactor we_can_set_the_inverse_on_this? to use a less bizarre name amongst ↵Jon Leighton2010-12-261-17/+7
| | | | other things
* If a has_many goes :through a belongs_to, and the foreign key of the ↵Jon Leighton2010-12-231-2/+6
| | | | belongs_to changes, then the has_many should be considered stale.
* Improved strategy for updating a belongs_to association when the foreign key ↵Jon Leighton2010-12-231-0/+9
| | | | changes. Rather than resetting each affected association when the foreign key changes, we should lazily check for 'staleness' (where fk does not match target id) when the association is accessed.
* Refactoring: replace the mix of variables like @finder_sql, @counter_sql, ↵Jon Leighton2010-10-301-11/+11
| | | | etc with just a single scope hash (created on initialization of the proxy). This is now used consistently across all associations. Therefore, all you have to do to ensure finding/counting etc is done correctly is implement the scope correctly.
* no need of nil checkNeeraj Singh2010-09-291-1/+1
|
* Adds title to activerecord/lib/active_record/associations/*Rizwan Reza2010-06-161-0/+1
|
* Provide a slightly more robust we_can_set_the_inverse_on_this? method for ↵Murray Steele2009-12-281-2/+7
| | | | | | | | polymorphic belongs_to associations. [#3520 state:resolved] Also add a new test for polymorphic belongs_to that test direct accessor assignment, not just .replace assignment. Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
* Add inverse polymorphic association support. [#3520 state:resolved]George Ogata2009-12-281-10/+29
| | | | Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
* Add primary_key option to belongs_to associationSzymon Nowak2009-07-151-1/+5
| | | | | | [#765 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
* Fix file permissionsTarmo Tänav2008-07-311-0/+0
| | | | Signed-off-by: Joshua Peek <josh@joshpeek.com>
* belongs_to polymorphic association assignments update the foreign_id and ↵Tim Harper2008-05-311-4/+2
| | | | | | | | | | | foreign_type fields regardless of whether the record being assigned is new or not. fixes the following scenarios: * I have validates_inclusion_of on the type field for a polymorphic belongs_to association. I assign a new record to the model's polymorphic relationship of the proper type. validation fails because the type field has not been updated. * I replace the value for a ppolymorphic association to a new record of another class. The type field still says its the previous class, and the id field points to the previous record as well. [#191 state:closed]
* Added :select option to has_one and belongs_to, remove unused :order option ↵josevalim2008-05-241-2/+3
| | | | | | | on belongs_to. Signed-off-by: Michael Koziarski <michael@koziarski.com> [#241 state:resolved]
* Backed out of new_record? to new? transformation as it would screw up ↵David Heinemeier Hansson2006-09-051-1/+1
| | | | | | existing models that did boolean calls on "new" attributes [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5018 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Deprecated ActiveRecord::Base.new_record? in favor of ↵David Heinemeier Hansson2006-09-051-1/+1
| | | | | | ActiveRecord::Base.new? (old version still works until Rails 2.0) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5017 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Use association's :conditions when eager loading. [jeremyevans0@gmail.com] ↵Rick Olson2006-03-161-1/+1
| | | | | | closes #4144 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3897 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Added Base.abstract_class? that marks which classes are not part of the ↵Rick Olson2006-03-161-1/+1
| | | | | | Active Record hierarchy. closes #3704 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3882 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Added preliminary support for join models [DHH] Added preliminary support ↵David Heinemeier Hansson2005-12-031-41/+21
| | | | | | for polymorphic associations [DHH] Refactored associations to use reflections to get DRYer, beware, major refactoring -- double check before deploying anything with this (all tests pass, but..) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3213 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
* Work-in progress for providing better join model support and polymorphic ↵David Heinemeier Hansson2005-12-021-0/+70
associations git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3209 5ecf4fe2-1ee6-0310-87b1-e25e094e27de