aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/singular_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Ensure `StatementCache#execute` never raises `RangeError`Ryuta Kamizono2019-01-181-2/+0
| | | | | | | | | | | | | Since 31ffbf8d, finder methods no longer raise `RangeError`. So `StatementCache#execute` is the only place to raise the exception for finder queries. `StatementCache` is used for simple equality queries in the codebase. This means that if `StatementCache#execute` raises `RangeError`, the result could always be regarded as empty. So `StatementCache#execute` just return nil in that range error case, and treat that as empty in the caller side, then we can avoid catching the exception in much places.
* Reuse AR::Association#find_target methodBogdan Gusiev2018-12-271-13/+1
|
* Revert "Merge pull request #34538 from bogdan/reuse-find-target"Ryuta Kamizono2018-11-281-6/+13
| | | | | | | This reverts commit f2ab8b64d4d46d7199f94c3e21021f414a4d259a, reversing changes made to b9c7305dbe57931a153a540d49ae5d469af61a14. Reason: `scope.take` is not the same with `scope.to_a.first`.
* Reuse code in AR::Association#find_targetBogdan Gusiev2018-11-271-13/+6
| | | | | | | Before this patch, singular and collection associations had different implementations of the #find_target method. This patch reuses the code properly through extending the low level methods.
* Clear QueryCache when reloading associationsChristophe Maximin2018-10-101-1/+1
|
* Initialization block is a part of `build_record`Ryuta Kamizono2018-06-041-6/+4
| | | | Should be done before `before_add` callbacks.
* Allow a belonging to object to be created from a new recordJolyon Pawlyn2018-05-011-4/+0
| | | | If a 'has one' object is created from a new record, an ActiveRecord::RecordNotSaved error is raised but this behavior was also applied to the reverse scenario.
* Passing `klass` to `StatementCache.new`Ryuta Kamizono2017-08-041-6/+4
| | | | | | Actually `StatementCache#execute` is always passed the same klass that the owner klass of the connection when the statement cache is created. So passing `klass` to `StatementCache.new` will make more DRY.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Don't cache `scope_for_create`Ryuta Kamizono2017-07-161-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | I investigated where `scope_for_create` is reused in tests with the following code: ```diff --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -590,6 +590,10 @@ def where_values_hash(relation_table_name = table_name) end def scope_for_create + if defined?(@scope_for_create) && @scope_for_create + puts caller + puts "defined" + end @scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys) end ``` It was hit only `test_scope_for_create_is_cached`. This means that `scope_for_create` will not be reused in normal use cases. So we can remove caching `scope_for_create` to respect changing `where_clause` and `create_with_value`.
* Fix `create_with` using both string and symbolRyuta Kamizono2017-07-161-3/+2
| | | | | | | This is related with #27680. Since `where_values_hash` keys constructed by `where` are string, so we need `stringify_keys` to `create_with_value` before merging it.
* 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
|
* Don't passing `klass.connection` to `AssociationScope`Ryuta Kamizono2017-06-291-1/+1
| | | | | Passing `klass.connection` is redundant because `AssociationScope` is passed an association itself and an association has `klass`.
* Prevent extra `scope` construction in `find_target`Ryuta Kamizono2017-06-181-1/+2
| | | | Because constructing `scope` is a little expensive.
* Raise on create for singular association when parent is unpersistedAlex Kitchens2017-06-081-0/+4
| | | | | | A collection association will raise on `#create_association` when the parent is unpersisted. A singular association should do the same. This addresses issue #29219.
* Remove deprecated force reload argument in association readersRafael Mendonça França2016-12-291-10/+2
|
* Fix CI failure caused by #25227 and #25280 were merged at the same timeRyuta Kamizono2016-12-101-1/+1
|
* Merge pull request #25280 from ↵Sean Griffin2016-12-101-0/+2
|\ | | | | | | | | kamipo/prevent_range_error_for_belongs_to_associations Prevent `RangeError` for `belongs_to` associations
| * Prevent `RangeError` for `belongs_to` associationsRyuta Kamizono2016-10-101-0/+2
| | | | | | | | | | | | | | | | Currently to access `belongs_to` associations raises a `RangeError` if foreign key attribute has out of range value. It should return a nil value rather than raising a `RangeError`. Fixes #20140.
* | Introduce `reload_<association>` reader for singular associations.Yves Senn2016-11-221-0/+7
|/ | | | | | | | | | | | | | | | This patch brings back the functionality of passing true to the association proxy. The behavior was deprecated with #20888 and scheduled for removal in Rails 5.1. The deprecation mentioned that instead of `Article.category(true)` one should use `article#reload.category`. Unfortunately the alternative does not expose the same behavior as passing true to the reader did. Specifically reloading the parent record throws unsaved changes and other caches away. Passing true only affected the association. This is problematic and there is no easy workaround. I propose to bring back the old functionality by introducing this new reader method for singular associations.
* Merge pull request #26380 from kamipo/pass_set_inverse_instance_blockKasper Timm Hansen2016-09-081-2/+2
|\ | | | | Pass `set_inverse_instance` block to `sc.execute` for `SingularAssociation`
| * Pass `set_inverse_instance` block to `sc.execute` for `SingularAssociation`Ryuta Kamizono2016-09-031-2/+2
| | | | | | | | | | | | | | Follow up to caa178c. caa178c updated all code which sets inverse instances on newly loaded associations to use block. But `SingularAssociation` was forgotten it.
* | Extract duplicated `create` and `create!` definition for associationRyuta Kamizono2016-09-031-8/+0
|/
* Avoid duplicated `set_inverse_instance` for target scopeRyuta Kamizono2016-08-031-7/+3
| | | | | | | | | Because `scope` (`target_scope`) is a `AssociationRelation`. `AssociationRelation` handles `set_inverse_instance`. https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/association_relation.rb#L31-L33 See also #26022.
* Mutating the result of Relation#to_a should not affect the relationMatthew Draper2016-02-211-1/+1
| | | | | | Clarifying this separation and enforcing relation immutability is the culmination of the previous efforts to remove the mutator method delegations.
* Delete needless `require 'active_support/deprecation'`yui-knk2015-10-201-2/+0
| | | | | When `require 'active_support/rails'`, 'active_support/deprecation' is automatically loaded.
* Skip statement cache on through association readerRafael Mendonça França2015-08-121-6/+1
| | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* Deprecate force association reload by passing truePrem Sichanugrist2015-07-151-0/+8
| | | | | | | | | | | | | | | | | | This is to simplify the association API, as you can call `reload` on the association proxy or the parent object to get the same result. For collection association, you can call `#reload` on association proxy to force a reload: @user.posts.reload # Instead of @user.posts(true) For singular association, you can call `#reload` on the parent object to clear its association cache then call the association method: @user.reload.profile # Instead of @user.profile(true) Passing a truthy argument to force association to reload will be removed in Rails 5.1.
* Fix `undefined method uncached` for polymorphic belongs_to #20426James Dabbs2015-06-131-1/+1
| | | | | | | Unitialized polymorphic `belongs_to` associations raise an error while attempting to reload, as they attempt to make an uncached reload, but don't have a klass to fetch uncachedly. In this case, `loaded?` should be `false` anyway.
* Isolate access to .default_scopes in ActiveRecord::Scoping::DefaultBen Woosley2015-03-121-2/+1
| | | | | | | | | | | Instead use .scope_attributes? consistently in ActiveRecord to check whether there are attributes currently associated with the scope. Move the implementation of .scope_attributes? and .scope_attributes to ActiveRecord::Scoping because they don't particularly have to do specifically with Named scopes and their only dependency, in the case of .scope_attributes?, and only caller, in the case of .scope_attributes is contained in Scoping.
* default scopes should break the cache on singulur_association.alfa-jpn2014-11-081-1/+2
| | | | fixes #17495
* break cache if we're inside a "scoping" call. fixes #17052Aaron Patterson2014-10-141-1/+6
| | | | | For now, we don't want to take "scoping" calls in to account when calculating cache keys for relations, so just opt-out.
* Skip StatementCache for eager loaded associations (Fixes #16761)Sammy Larbi2014-09-041-1/+1
| | | | | | Eagerly loaded collection and singular associations are ignored by the StatementCache, which causes errors when the queries they generate reference columns that were not eagerly loaded. This commit skips the creation of the StatementCache as a fix for these scenarios.
* use statement cache for belongs_to relationsAaron Patterson2014-04-221-1/+12
|
* extract record fetching to a method for belongs_toAaron Patterson2014-04-221-1/+5
|
* [Active Record] Renamed private methods create_record and update_recordPrathamesh Sonpatki2014-02-201-3/+3
| | | | | | This is to ensure that they are not accidentally called by the app code. They are renamed to _create_record and _update_record respectively. Closes #11645
* `has_one` and `belongs_to` accessors don't add ORDER BY to the queries anymore.Rafael Mendonça França2014-01-211-1/+1
| | | | | | | | | | Since Rails 4.0, we add an ORDER BY in the `first` method to ensure consistent results among different database engines. But for singular associations this behavior is not needed since we will have one record to return. As this ORDER BY option can lead some performance issues we are removing it for singular associations accessors. Fixes #12623.
* remove the nil check from set_inverse_instanceAaron Patterson2013-12-121-1/+3
| | | | | methods that call set_inverse_instance with a record will not have to pay the cost of a nil check on every call
* Remove useless comment and white spaces :scissors: [ci skip]Carlos Antonio da Silva2013-09-011-1/+0
|
* This is comment for singular association.kennyj2012-11-021-1/+1
|
* Remove mass_assignment_options from ActiveRecordGuillermo Iguaran2012-09-161-8/+8
|
* s/scoped/scope/Jon Leighton2012-08-011-2/+2
|
* Ensure that the foreign key gets set when doing record.create_association or ↵Jon Leighton2011-07-081-2/+11
| | | | record.create_association. Fixes #1960.
* Assign the association attributes to the associated record before the ↵Jon Leighton2011-06-301-2/+1
| | | | before_initialize callback of the record runs. Fixes #1842.
* Don't pass a block as we are yieldingAndrew White2011-05-171-1/+1
|
* Add block setting of attributes to singular associationsAndrew White2011-05-171-5/+6
|
* Pass the attribute and option hashes to build_associationAndrew White2011-05-171-6/+6
| | | | | | | The build_association method was added as an API for plugins to hook into in 1398db0. This commit restores this API and the ability to override class.new to return a subclass based on a virtual attribute in the attributes hash.
* Don't use mass-assignment protection when setting foreign keys or ↵Jon Leighton2011-05-121-9/+9
| | | | association conditions on singular associations. Fixes #481 (again).
* singular and collection relations in AR can now specify mass-assignment ↵Josh Kalderimis2011-05-011-8/+8
| | | | security options (:as and :without_protection) in build, create and create! methods.