aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association.rb
Commit message (Collapse)AuthorAgeFilesLines
* Allow `autosave: true` to be used with inverse ofSean Griffin2016-11-011-0/+9
| | | | | | | | | | | | | | | | | With the changes in #25337, double save bugs are pretty much impossible, so we can just lift this restriction with pretty much no change. There were a handful of cases where we were relying on specific quirks in tests that had to be updated. The change to has_one associations was due to a particularly interesting test where an autosaved has_one association was replaced with a new child, where the child failed to save but the test wanted to check that the parent id persisted to `nil`. I think this is almost certainly the wrong behavior, and I may change that behavior later. But ultimately the root cause was because we never remove the parent in memory when nullifying the child. This makes #23197 no longer needed, but it is what we'll do to fix some issues on 5.0 Close #23197
* Deprecate the behavior of AR::Dirty inside of after_(create|update|save) ↵Sean Griffin2016-11-011-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Extract duplicated `create` and `create!` definition for associationRyuta Kamizono2016-09-031-0/+8
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-1/+1
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-1/+1
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Give more context from `AssociationMismatchError`Sean Griffin2016-05-121-1/+2
| | | | | | | The error message that we give today makes this error difficult to debug if you receive it. I have no clue why we're printing the object ID of the class (the commit doesn't give context), but I've left it as it was deliberate.
* start hiding the `scope_chain` data structureAaron Patterson2016-03-041-1/+1
| | | | | Introduce a predicate method that doesn't need to build a scope chain, but also hides the data structure used for representing the scope chain.
* Except keys of `build_record`'s argument from `create_scope` in ↵yui-knk2015-11-161-3/+6
| | | | | | | | | | | | | | initialize_attributes If argument of `build_record` has key and value which is same as default value of database, we should also except the key from `create_scope` in `initialize_attributes`. Because at first `build_record` initialize record object with argument of `build_record`, then assign attributes derived from Association's scope. In this case `record.changed` does not include the key, which value is same as default value of database, so we should add the key to except list. Fix #21893.
* Skip statement cache on through association readerRafael Mendonça França2015-08-121-0/+8
| | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* thrown ActiveRecord::AssociationTypeMismatch when assigning a wrong value ↵Diego Carrion2015-06-221-3/+6
| | | | | for a namespaced association fixes #20541
* Doc fix about association hierarchykeepcosmos2015-03-151-2/+2
|
* Inject the `PredicateBuilder` into the `Relation` instanceSean Griffin2014-12-261-1/+1
| | | | | | | Construction of relations can be a hotspot, we don't want to create one of these in the constructor. This also allows us to do more expensive things in the predicate builder's constructor, since it's created once per AR::Base subclass
* reuse available collection? check instead of macroeileencodes2014-06-091-1/+1
| | | | | | | Reflection has an available method that is used to check if the reflection is a collection. Any :has_many macro is considered a collection and `collection?` should be used instead of `macro == :has_many`.
* add has_one? method and reuse instead of checking macroeileencodes2014-06-091-1/+1
| | | | | | Instead of checking for `macro == :has_one` throughout the codebase we can create a `has_one?` method to match the `belongs_to?`, `polymorphic?` and other methods.
* Merge pull request #15210 from arthurnn/fix_hbtm_reflectionArthur Neves2014-05-241-1/+1
| | | | | | | | | 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
* Prevent foreign_key_for? from type casting all attributesWojtek Kruszewski2014-02-211-1/+1
|
* make a singleton for AssociationScopeAaron Patterson2014-02-141-1/+1
| | | | | AssociationScope no longer maintains state, so we're safe to keep a singleton and save on GC time
* pass the association and connection to the scope methodAaron Patterson2014-02-141-1/+1
|
* remove the nil check from set_inverse_instanceAaron Patterson2013-12-121-1/+2
| | | | | methods that call set_inverse_instance with a record will not have to pay the cost of a nil check on every call
* fix typo, "state_state" should be "stale_state" [ci skip]Scott M2013-12-071-1/+1
|
* Checks to see if the record contains the foreign_key to set the inverse ↵Edo Balvers2013-11-161-1/+6
| | | | automatically
* add inversed accessor to association classDmitry Polushkin2013-10-131-2/+3
|
* inversed instance should not be reloaded after stale state was changedDmitry Polushkin2013-10-131-2/+5
| | | check at association reader that record is inverted and should not be reloaded because of stale was changed at target record
* remove HABTM associationsAaron Patterson2013-10-021-1/+0
|
* add a specific factory method rather than using newAaron Patterson2013-07-231-1/+1
|
* Removed deprecated method scopedNeeraj Singh2013-07-011-5/+0
|
* Simplify/fix implementation of default scopesJon Leighton2013-06-281-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The previous implementation was necessary in order to support stuff like: class Post < ActiveRecord::Base default_scope where(published: true) scope :ordered, order("created_at") end If we didn't evaluate the default scope at the last possible moment before sending the SQL to the database, it would become impossible to do: Post.unscoped.ordered This is because the default scope would already be bound up in the "ordered" scope, and therefore wouldn't be removed by the "Post.unscoped" part. In 4.0, we have deprecated all "eager" forms of scopes. So now you must write: class Post < ActiveRecord::Base default_scope { where(published: true) } scope :ordered, -> { order("created_at") } end This prevents the default scope getting bound up inside the "ordered" scope, which means we can now have a simpler/better/more natural implementation of default scoping. A knock on effect is that some things that didn't work properly now do. For example it was previously impossible to use #except to remove a part of the default scope, since the default scope was evaluated after the call to #except.
* enhanced comments for foreign_key_present? methodNeeraj Singh2013-05-231-3/+4
|
* Set the inverse when association queries are refinedJon Leighton2013-05-101-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | Suppose Man has_many interests, and inverse_of is used. Man.first.interests.first.man will correctly execute two queries, avoiding the need for a third query when Interest#man is called. This is because CollectionAssociation#first calls set_inverse_instance. However Man.first.interests.where("1=1").first.man will execute three queries, even though this is obviously a subset of the records in the association. This is because calling where("1=1") spawns a new Relation object from the CollectionProxy object, and the Relation has no knowledge of the association, so it cannot set the inverse instance. This commit solves the problem by making relations spawned from CollectionProxies return a new Relation subclass called AssociationRelation, which does know about associations. Records loaded from this class will get the inverse instance set properly. Fixes #5717. Live commit from La Conf! :sparkles:
* Do not overwrite manually built records during one-to-one nested attribute ↵Olek Janiszewski2013-05-031-4/+8
| | | | | | | | | | | | | | | | | | | | | assignment For one-to-one nested associations, if you build the new (in-memory) child object yourself before assignment, then the NestedAttributes module will not overwrite it, e.g.: class Member < ActiveRecord::Base has_one :avatar accepts_nested_attributes_for :avatar def avatar super || build_avatar(width: 200) end end member = Member.new member.avatar_attributes = {icon: 'sad'} member.avatar.width # => 200
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-04-111-1/+1
|\ | | | | | | | | Conflicts: guides/source/action_mailer_basics.md
| * minor copy editingNeeraj Singh2013-03-301-1/+1
| |
* | Merge pull request #9996 from mikz/masterJon Leighton2013-04-051-0/+1
|\ \ | |/ |/| Association with inverse_of does not set the parent in association building block
| * fix inverse_of association in block of new childMichal Cichra2013-04-011-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes inconsistency when building children of association which has inverse_of set properly. When creating new association object with a block: parent.association.build do |child| child.parent.equal?(parent) # false end So the block the `child.parent` did not point to the same object. But when the object is created it points to same instance: child = parent.association.build child.parent.equal?(parent) # true
* | Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-03-301-1/+2
|\ \ | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb activerecord/test/cases/adapter_test.rb guides/source/testing.md [ci skip]
| * | explain method invertible_for [ci skip]Neeraj Singh2013-03-291-1/+2
| | |
* | | Do not use deprecate method [ci skip]Neeraj Singh2013-03-291-1/+1
| | |
* | | fix broken sentence [ci skip]Neeraj Singh2013-03-291-1/+1
| |/ |/|
* | Adding a bang to method name of raise_on_type_mismatch.wangjohn2013-03-211-1/+1
|/
* No need to send public methodsAkira Matsuda2013-02-261-1/+1
|
* prevent mass assignment of polymorphic type when using `build`Yves Senn2012-11-221-1/+2
| | | | Closes #8265
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-11-031-1/+1
|\ | | | | | | | | | | | | Conflicts: actionpack/lib/action_controller/metal/mime_responds.rb activerecord/lib/active_record/attribute_methods.rb guides/source/working_with_javascript_in_rails.md
| * fix a typo in comments to ActiveRecord::Associations::Association.stale_stateAndrii Dovgaliuk2012-10-241-1/+1
| |
* | Make caller attribute in deprecation methods optionalAlexey Gaziev2012-10-301-1/+1
| |
* | Provide a call stack for deprecation warnings where needed.Nikita Afanasenko2012-10-291-1/+1
|/ | | | It's sometimes hard to quickly find where deprecated call was performed, especially in case of migrating between Rails versions. So this is an attempt to improve the call stack part of the warning message by providing caller explicitly.
* Fix destructive side effects from marshaling an association caused by ↵Jeremy Kemper2012-09-251-5/+2
| | | | 65843e1acc0c8d285ff79f8c9c49d4d1215440be
* Remove mass_assignment_options from ActiveRecordGuillermo Iguaran2012-09-161-3/+3
|
* removes usage of Object#in? from the code base (the method remains defined ↵Xavier Noria2012-08-061-1/+1
| | | | | | | | | | | | | | | | | | | by Active Support) Selecting which key extensions to include in active_support/rails made apparent the systematic usage of Object#in? in the code base. After some discussion in https://github.com/rails/rails/commit/5ea6b0df9a36d033f21b52049426257a4637028d we decided to remove it and use plain Ruby, which seems enough for this particular idiom. In this commit the refactor has been made case by case. Sometimes include? is the natural alternative, others a simple || is the way you actually spell the condition in your head, others a case statement seems more appropriate. I have chosen the one I liked the most in each case.
* load active_support/deprecation in active_support/railsXavier Noria2012-08-021-1/+0
|
* load active_support/core_ext/object/inclusion in active_support/railsXavier Noria2012-08-021-1/+0
|