aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
Commit message (Collapse)AuthorAgeFilesLines
* Bugfix collection association #create method …Bogdan Gusiev2015-11-231-0/+6
| | | | | When same association is loaded in the model creation callback The new object is inserted into association twice
* Except keys of `build_record`'s argument from `create_scope` in ↵yui-knk2015-11-161-1/+14
| | | | | | | | | | | | | | 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.
* Merge pull request #22125 from k0kubun/left_joinsSean Griffin2015-10-301-0/+5
|\ | | | | Alias left_joins to left_outer_joins
| * Alias left_joins to left_outer_joinsTakashi Kokubun2015-10-311-0/+5
| |
* | Fix test failures caused by #12071Sean Griffin2015-10-301-10/+10
|/ | | | | | This assumes only one query was ever executed, but it appears to sometimes be loading schema information. We can just look at the array of queries, rather than the "first" one that was run
* Merge pull request #12071 from Crunch09/outer_joinsSean Griffin2015-10-301-0/+74
|\ | | | | | | added ActiveRecord::Relation#outer_joins
| * added ActiveRecord::Relation#left_outer_joinsFlorian Thomas2015-05-191-0/+74
| | | | | | | | | | | | Example: User.left_outer_joins(:posts) => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
* | Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`Sean Griffin2015-10-291-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit, if `ActiveRecord::Base.belongs_to_required_by_default` is set to `true`, then creating a record through `has_and_belongs_to_many` fails with the cryptic error message `Left side must exist`. This is because `inverse_of` isn't working properly in this case, presumably since we're doing trickery with anonymous classes in the middle. Rather than following this rabbit hole to try and get `inverse_of` to work in a case that we know is not publicly supported, we can just turn off this validation to match the behavior of 4.2 and earlier.
* | Never pass `nil` to `order`Sean Griffin2015-10-291-6/+0
| | | | | | | | | | | | | | | | This is part of a refactoring to make it easier to allow `order` to use sanitize like just about everything else on relation. The deleted test doesn't give any reasoning as to why passing `nil` to `order` needs to be supported, and it's rather nonsensical. I can almost see allowing an empty string being passed (though I'm tempted to just disallow it...)
* | Revert "Revert "Merge pull request #22026 from ↵Yves Senn2015-10-291-0/+18
| | | | | | | | | | | | | | | | | | akihiro17/fix-preload-association"" This reverts commit 5243946017d09afff4d70d273b0fcdfd41a4b22a. This fixes an issue with the build where tests would fail on mysql and postgresql due to different ordering.
* | Revert "Merge pull request #22026 from akihiro17/fix-preload-association"Sean Griffin2015-10-291-18/+0
| | | | | | | | | | | | | | This reverts commit 6dc6a0b17cfaf7cb6aa2b1c163b6ca141b538a8e, reversing changes made to ec94f00ba3cf250eb54fc5b7a5e3ed4b90164f34. This pull request broke the build.
* | Merge pull request #19501 from ccutrer/dry_sti_subclass_finding2Sean Griffin2015-10-291-2/+2
|\ \ | | | | | | | | | DRY up STI subclass logic
| * \ Fix merge conflicts from #19501Sean Griffin2015-10-291-2/+2
| |\ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | I'm making this commit separately because this has failing tests and style nitpicks that I'd like to make as individual commits, to make the changes I'm making explicit. We still want a single merge commit at the end, however.
| | * | DRY up STI subclass logicCody Cutrer2015-03-241-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | the newer method used for discriminating new records did not use the older and more robust method used for instantiating existing records, but did have a better post-check to ensure the sublass was in the hierarchy. so move the descendants check to find_sti_class, and then simply call find_sti_class from subclass_from_attributes now with fixed specs
* | | | Set `scope.reordering_value` to `true` if :reordering values are specifiedakihiro172015-10-301-0/+18
|/ / / | | | | | | | | | | | | | | | We should call `scope.order!` and set `scope.reordering_value` to `true` if :reordering values are specified Fixes #21886
* | | Merge pull request #18548 from ↵Sean Griffin2015-10-281-0/+41
|\ \ \ | | | | | | | | | | | | | | | | | | | | sebjacobs/support-bidirectional-destroy-dependencies Add support for bidirectional destroy dependencies
| * | | Add support for bidirectional destroy dependenciesSeb Jacobs2015-01-161-0/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit if you defined a bidirectional relationship between two models with destroy dependencies on both sides, a call to `destroy` would result in an infinite callback loop. Take the following relationship. class Content < ActiveRecord::Base has_one :content_position, dependent: :destroy end class ContentPosition < ActiveRecord::Base belongs_to :content, dependent: :destroy end Calling `Content#destroy` or `ContentPosition#destroy` would result in an infinite callback loop. This commit changes the behaviour of `ActiveRecord::Callbacks#destroy` so that it guards against subsequent callbacks. Thanks to @zetter for demonstrating the issue with failing tests[1]. [1] rails#13609
* | | | Merge pull request #18383 from ↵Rafael Mendonça França2015-10-271-0/+19
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | scambra/habtm-with-where-includes-16032-for-master Includes HABTM returns correct size now
| * | | | Includes HABTM returns correct size now. It's caused by the join dependencySergio Cambra2015-01-071-0/+19
| |/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | only instantiates one HABTM object because the join table hasn't a primary key. Updated commit from @bigxiang commit dbaa837 Fixes #16032. Examples: before: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 1 after: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 3
* | | | Tweaked wording used in some tests.Sebastian McKenzie2015-10-251-4/+4
| | | |
* | | | Fix merge conflicts for #19938Sean Griffin2015-10-201-0/+12
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | This is a separate commit, as it is not just a changelog conflict. Want to point out the changes in the code
| * | | | Fix for activerecord join dependency instantiate bugMehmet Emin İNAÇ2015-05-041-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | use only object_id instead parent class and parent id test cases assert_equal use table name in references fix minor problems
* | | | | Reorder application of has_many association constraints.jbranchaud2015-10-191-0/+20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | With `unscope!` called last, it undoes `where` constraints of the same value when the `where` is chained after the `unscope`. This is what a `rewhere` does. This is undesirable behavior. The included tests demonstrate both the `unscope(...).where(...)` behavior as well as the direct use of `rewhere(...)`. This is in reference to #21955.
* | | | | Fix regression in inverse_of on through associationseileencodes2015-09-261-0/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `inverse_of` on through associations was accidently removed/caused to stop working in commit f8d2899 which was part of a refactoring on `ThroughReflection`. To fix we moved `inverse_of` and `check_validity_of_inverse!` to the `AbstractReflection` so it's available to the `ThroughReflection` without having to dup any methods. We then need to delegate `inverse_name` method in `ThroughReflection`. `inverse_name` can't be moved to `AbstractReflection` without moving methods that set the instance variable `@automatic_inverse_of`. This adds a test that ensures that `inverse_of` on a `ThroughReflection` returns the correct class name, and the correct record for the inverse relationship. Fixes #21692
* | | | | Merge pull request #21550 from didacte/unscope-associationsSean Griffin2015-09-241-0/+21
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | ActiveRecord: use association's `unscope` when preloading
| * | | | | Include association's `unscope` when preloadingJimmy Bourassa2015-09-091-0/+21
| | | | | |
* | | | | | Don't rely on subsecond precision being applied in testsSean Griffin2015-09-232-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When I originally reviewed the #20317, I believe these changes were present, but it appears that it was later updated so that they were removed. Since Travis hadn't re-run the build, this slipped through.
* | | | | | Merge pull request #21581 from ronakjangir47/restrict_with_errorYves Senn2015-09-172-0/+37
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | `restrict_with_error` message will now respect owner’s human name
| * | | | | | `restrict_with_error` message will now respect owner’s human name in any ↵Ronak Jangir2015-09-122-0/+37
|/ / / / / / | | | | | | | | | | | | | | | | | | locale [kuboon & Ronak Jangir]
* / / / / / HasManyAssociation: moved half of counter cache code to reflectionBogdan Gusiev2015-09-031-1/+1
|/ / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Current implementation has a lot of utility methods that accept reflection call a lot of methods on it and exit. E.g. has_counter_cache?(reflection) It causes confusion and inability to cache result of the method even through it always returns the same result for the same reflection object. It can be done easier without access to the association context by moving code into reflection itself. e.g. reflection.has_counter_cache? Reflection is less complex object than association so moving code there automatically makes it simplier to understand.
* | | | | Removed duplicate require ‘models/computer’Ronak Jangir2015-08-261-1/+0
| | | | |
* | | | | Removed mocha from Active Record Part 1Ronak Jangir2015-08-253-35/+42
| | | | |
* | | | | Only nullify persisted has_one target associationsAgis-2015-08-241-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since after 87d1aba3c `dependent: :destroy` callbacks on has_one assocations run *after* destroy, it is possible that a nullification is attempted on an already destroyed target: class Car < ActiveRecord::Base has_one :engine, dependent: :nullify end class Engine < ActiveRecord::Base belongs_to :car, dependent: :destroy end > car = Car.create! > engine = Engine.create!(car: car) > engine.destroy! # => ActiveRecord::ActiveRecordError: cannot update a > destroyed record In the above case, `engine.destroy!` deletes `engine` and *then* triggers the deletion of `car`, which in turn triggers a nullification of `engine.car_id`. However, `engine` is already destroyed at that point. Fixes #21223.
* | | | | Merge pull request #21284 from prakashlaxkar/argument_error_testsYves Senn2015-08-181-0/+8
| | | | | | | | | | | | | | | | | | | | Correct error message in Standard American english and add a test cas…
* | | | | Merge pull request #19683 from tristang/require-option-for-counter-cacheRafael Mendonça França2015-08-132-0/+40
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | Require explicit counter_cache option for has_many
| * | | | | Add tests for associations without counter_cacheTristan Gamilis2015-04-092-0/+40
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Assert that counter_cache behaviour is not used on belongs_to or has_many associations if the option is not given explicitly.
* | | | | | use `assert_not` instead of `refute` as mentioned in our guides.Yves Senn2015-08-131-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As described in the "Follow Coding Conventions" section in our contribution guide (http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions) we favor `assert_not` over `refute`. While we don't usually make stylistic changes on it's own I opted to do it in this case. The reason being that test cases are usually copied as a starting point for new tests. This results in a spread of `refute` in files that have been using it already.
* | | | | | Skip statement cache on through association readerRafael Mendonça França2015-08-122-9/+58
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* | | | | | use correct DB connection for generated HABTM tableMatt Hanlon2015-08-071-0/+12
| | | | | |
* | | | | | Use memoization for collection associations ids readerMehmet Emin İNAÇ2015-08-031-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #21082 remove extra space
* | | | | | Merge pull request #20849 from vngrs/misleading_nested_exceptionsRafael Mendonça França2015-07-272-1/+8
|\ \ \ \ \ \ | | | | | | | | | | | | | | Fix misleading errors for has_one through relations
| * | | | | | Fix misleading errors for has_one through relationsMehmet Emin İNAÇ2015-07-222-1/+8
| | | | | | |
* | | | | | | `destroy` shouldn't raise when child associations fail to saveSean Griffin2015-07-241-0/+30
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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).
* | | | | | Deprecate and rename the keys for association restrict_dependent_destroyRoque Pinel2015-07-202-0/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously `has_one` and `has_many` associations were using the `one` and `many` keys respectively. Both of these keys have special meaning in I18n (they are considered to be pluralizations) so by renaming them to `has_one` and `has_many` we make the messages more explicit and most importantly they don't clash with linguistical systems that need to validate translation keys (and their pluralizations). The `:'restrict_dependent_destroy.one'` key should be replaced with `:'restrict_dependent_destroy.has_one'`, and `:'restrict_dependent_destroy.many'` with `:'restrict_dependent_destroy.has_many'`. [Roque Pinel & Christopher Dell]
* | | | | | Silence deprecation warning from force reloadPrem Sichanugrist2015-07-167-87/+98
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We deprecate the support for passing an argument to force reload in 6eae366d0d2e5d5211eeaf955f56bd1dc6836758. That led to several deprecation warning when running Active Record test suite. This commit silence the warnings by properly calling `#reload` on the association proxy or on the association object instead. However, there are several places that `ActiveSupport::Deprecation.silence` are used as those tests actually tests the force reload functionality and will be removed once `master` is targeted next minor release (5.1).
* | | | | | Deprecate force association reload by passing truePrem Sichanugrist2015-07-156-0/+36
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* | | | | | Correct through associations using scopesSean Griffin2015-06-301-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The changes introduced to through associations in c80487eb were quite interesting. Changing `relation.merge!(scope)` to `relation = relation.merge(scope)` should in theory never cause any changes in behavior. The subtle breakage led to a surprising conclusion. The old code wasn't doing anything! Since `merge!` calls `instance_exec` when given a proc, and most scopes will look something like `has_many :foos, -> { where(foo: :bar) }`, if we're not capturing the return value, it's a no-op. However, removing the `merge` causes `unscope` to break. While we're merging in the rest of the chain elsewhere, we were never merging in `unscope` values, causing a breakage on associations where a default scope was being unscoped in an association scope (yuk!). This is subtly related to #20722, since it appears we were previously relying on this mutability. Fixes #20721. Fixes #20727.
* | | | | | Merge pull request #20552 from jamesdabbs/belongs-to-polymorphic-force-reloadYves Senn2015-06-231-0/+2
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | Fix `undefined method uncached` for polymorphic belongs_to #20426
| * | | | | | Fix `undefined method uncached` for polymorphic belongs_to #20426James Dabbs2015-06-131-0/+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.
* | | | | | | Merge pull request #20545 from dcrec1/20541Yves Senn2015-06-231-0/+26
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | thrown ActiveRecord::AssociationTypeMismatch when assigning a wrong value for a namespaced association