aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
Commit message (Collapse)AuthorAgeFilesLines
* Add test for collection *_ids= setter when association primary key setDominic Cleal2016-11-241-0/+14
| | | | | | | | Fixes casting of IDs to the data type of the association primary key, rather than then the data type of the model's primary key. (Tests use a string primary key on the association, rather than an int.) Tests issue #20995
* Restore RecordNotFound when *_ids= can't find records by IDDominic Cleal2016-11-241-1/+2
| | | | | | | | | | | | | | | | 9c9fb19 changed the behaviour of the _ids= setters for associations to raise an AssociationTypeMismatch when unknown IDs are given: Class: <ActiveRecord::AssociationTypeMismatch> Message: <"Developer(#43811860) expected, got NilClass(#16732720)"> This restores the original ActiveRecord::RecordNotFound exception with a much clearer error message: Class: <ActiveRecord::RecordNotFound> Message: <"Couldn't find all Developers with 'id': (1, -9999) [WHERE \"contracts\".\"company_id\" = ?] (found 1 results, but was looking for 2)"> Fixes #25719
* Introduce `reload_<association>` reader for singular associations.Yves Senn2016-11-222-0/+20
| | | | | | | | | | | | | | | | 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.
* Respect new records for `CollectionProxy#uniq`Ryuta Kamizono2016-11-133-2/+3
| | | | | | | | | | | | | Currently if `CollectionProxy` has more than one new record, `CollectionProxy#uniq` result is incorrect. And `CollectionProxy#uniq` was aliased to `distinct` in a1bb6c8b06db. But the `uniq` method and the `SELECT DISTINCT` method are different methods. The doc in `CollectionProxy` is for the `SELECT DISTINCT` method, not for the `uniq` method. Therefore, reverting the alias in `CollectionProxy` to fix the inconsistency and to have the both methods.
* Fixes TypeError Exception when cache counter value equals nil (#26940)Daniel E. Garcia Shulman2016-11-021-0/+5
| | | | | | * Fixes TypeError when cache counter value equals nil * Test case for counter cache on unloaded has_many association
* Allow `autosave: true` to be used with inverse ofSean Griffin2016-11-011-1/+1
| | | | | | | | | | | | | | | | | 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
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-2913-59/+59
|
* Merge PR #19759Arthur Neves2016-10-281-0/+13
|\ | | | | | | Fix for has_and_belongs_to_many & has_many_through associations
| * Fix for has_and_belongs_to_many & has_many_through associations while ↵Mehmet Emin İNAÇ2016-02-131-0/+13
| | | | | | | | | | | | | | | | partial_writes is false This will fix #19663 Also with this fix, active record does not fire unnecassary update queries while partial_writes is true
* | Fix HABTM associations join table resolver bug on constants and symbolsMehmet Emin İNAÇ2016-10-271-1/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Using Constant and symbol class_name option for associations are valid but raises exception on HABTM associations. There was a test case which tries to cover symbol class_name usage but doesn't cover correctly. Fixed both symbol usage and constant usage as well. These are all working as expected now; ``` has_and_belongs_to_many :foos, class_name: 'Foo' has_and_belongs_to_many :foos, class_name: :Foo has_and_belongs_to_many :foos, class_name: Foo ``` Closes #23767
* | let Regexp#match? be globally availableXavier Noria2016-10-271-1/+0
| | | | | | | | | | | | Regexp#match? should be considered to be part of the Ruby core library. We are emulating it for < 2.4, but not having to require the extension is part of the illusion of the emulation.
* | Fix regression caused due to removal of select method from CollectionAssociationPrathamesh Sonpatki2016-10-221-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - CollectionAssociation#select was removed in https://github.com/rails/rails/pull/25989 in favor of QueryMethods#select but it caused a regression when passing arguments to select and a block. - This used to work earlier in Rails 4.2 and Rails 5. See gist https://gist.github.com/prathamesh-sonpatki/a7df922273473a77dfbc742a4be4b618. - This commit restores the behavior of Rails 4.2 and Rails 5.0.0 to allow passing arguments and block at the same time but also deprecates it. - Because, these arguments do not have any effect on the output of select when select is used with a block. - Updated documentation to remove the example passing arguments and block at the same time to `CollectionProxy#select`.
* | Don't skip in-memory insertion of associations when loaded in validateSean Griffin2016-09-291-3/+31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This was caused by 6d0d83a33f59d9415685852cf77818c41e2e2700. While the bug it's trying to fix is handled if the association is loaded in an after_(create|save) callback, it doesn't handle any cases that load the association before the persistence takes place (validation, or before_* filters). Instead of caring about the timing of persistence, we can just ensure that we're not double adding the record instead. The test from that commit actually broke, but it was not because the bug has been re-introduced. It was because `Bulb` in our test suite is doing funky things that look like STI but isn't STI, so equality comparison didn't happen as the loaded model was of a different class. Fixes #26661.
* | improve error message when include assertions failMichael Grosser2016-09-168-69/+69
| | | | | | | | | | | | assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
* | Ensure that inverse associations are set before running callbacksSean Griffin2016-08-311-0/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
* | Don't unnecessarily load a belongs_to when saving.James Coleman2016-08-261-0/+6
| | | | | | | | | | | | | | | | Previously, if the the association was previously loaded and then the foreign key changed by itself, a #save call would trigger a load of the new associated record during autosave. This is unnecessary and the autosave code (in that case) didn't use the loaded record anyways.
* | Merge pull request #24099 from k0kubun/preserve-readonlyRafael Mendonça França2016-08-181-2/+20
|\ \ | | | | | | | | | Preserve readonly flag only for readonly association
| * | Preserve readonly flag only for readonly associationTakashi Kokubun2016-07-301-2/+20
| | | | | | | | | | | | Fixes #24093
* | | Merge pull request #26009 from kamipo/fix_inconsistent_finder_methods_signatureRafael França2016-08-171-9/+0
|\ \ \ | | | | | | | | Fix inconsistent the signature of finder methods for collection association
| * | | Fix inconsistent the signature of finder methods for collection associationRyuta Kamizono2016-08-161-9/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | `#second`, `#third`, etc finder methods was added in 03855e790de2224519f55382e3c32118be31eeff. But the signature of these methods is inconsistent with the original finder methods. And also the signature of `#first` and `#last` methods is different from the original. This commit fixes the inconsistency.
* | | | Merge pull request #26021 from ↵Rafael França2016-08-171-0/+40
|\ \ \ \ | | | | | | | | | | | | | | | | | | | | kamipo/finder_bang_method_should_call_non_bang_method Finder bang method should call non bang method
| * | | | Finder bang method should call non bang methodRyuta Kamizono2016-08-161-0/+40
| |/ / / | | | | | | | | | | | | Otherwise CollectionProxy's bang methdos cannot respect dirty target.
* / / / Fix count which would sometimes force a DISTINCTMaxime Lapointe2016-08-161-8/+17
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The current behaviour of checking if there is a LEFT OUTER JOIN arel node to detect if we are doing eager_loading is wrong. This problem wasn't frequent before as only some pretty specific cases would add a LEFT OUTER JOIN arel node. However, the recent new feature left_outer_joins also add this node and made this problem happen frequently. Since in the perform_calculation function, we don't have access to eager_loading information, I had to extract the logic for the distinct out to the calculate method. As I was in the file for left_outer_join tests, I fixed a few that had bugs and I replaced some that were really weak with something that will catch more issues. In relation tests, the first test I changed would have failed if it had validated the hash returned by count instead of just checking how many pairs were in it. This is because this merge of join currently transforms the join node into an outer join node, which then made count do a distinct. So before this change, the return was {1=>1, 4=>1, 5=>1}.
* | | Merge pull request #26097 from Dagnan/fix_recordnotfound_args_rebasedRafael França2016-08-161-0/+12
|\ \ \ | | | | | | | | When calling association.find RecordNotFound is now raised with the s…
| * | | When calling association.find RecordNotFound is now raised with the same ↵Michel Pigassou2016-08-131-0/+12
| | | | | | | | | | | | | | | | argument as when we do it in Record.find (primary_key, id and model).
* | | | Merge pull request #26162 from kamipo/take_respects_dirty_targetRafael França2016-08-161-4/+8
|\ \ \ \ | | | | | | | | | | `CollectionProxy#take` should respect dirty target
| * | | | `CollectionProxy#take` should respect dirty targetRyuta Kamizono2016-08-141-4/+8
| |/ / / | | | | | | | | | | | | | | | | `#first`, `#second`, ..., `#last` methods respects dirty target. But `#take` doesn't respect it. This commit fixes the inconsistent behavior.
* / / / Add three new rubocop rulesRafael Mendonça França2016-08-1615-127/+127
|/ / / | | | | | | | | | | | | | | | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* | | Fix broken alignments caused by auto-correct commit 411ccbdRyuta Kamizono2016-08-101-1/+2
| | | | | | | | | | | | Hash syntax auto-correcting breaks alignments. 411ccbdab2608c62aabdb320d52cb02d446bb39c
* | | Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty linesRyuta Kamizono2016-08-075-6/+0
| | |
* | | applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
| | |
* | | normalizes indentation and whitespace across the projectXavier Noria2016-08-062-129/+129
| | |
* | | remove redundant curlies from hash argumentsXavier Noria2016-08-062-7/+5
| | |
* | | modernizes hash syntax in activerecordXavier Noria2016-08-0617-671/+671
| | |
* | | applies new string literal convention in activerecord/testXavier Noria2016-08-0620-961/+961
|/ / | | | | | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* | Merge pull request #25767 from ↵Rafael França2016-07-271-0/+6
|\ \ | | | | | | | | | | | | kamipo/association_name_is_the_same_as_join_table_name Correctly return `associated_table` when `associated_with?` is true
| * | Correctly return `associated_table` when `associated_with?` is trueRyuta Kamizono2016-07-101-0/+6
| | | | | | | | | | | | | | | | | | | | | `AssociationQueryHandler` requires `association` initialized `TableMetadata` even if `table_name == arel_table.name`. Fixes #25689.
* | | Add an assertion to the tests that should be loaded the target.Ryuta Kamizono2016-07-271-0/+38
| | | | | | | | | | | | These test cases tests exactly mutating loaded target.
* | | Make `force_signal37_to_load_all_clients_of_firm` to privateRyuta Kamizono2016-07-261-4/+6
| | | | | | | | | | | | This is not a test case.
* | | adds missing requiresXavier Noria2016-07-241-0/+1
| | |
* | | systematic revision of =~ usage in ARXavier Noria2016-07-232-6/+6
| | | | | | | | | | | | | | | Where appropriatei, prefer the more concise Regexp#match?, String#include?, String#start_with?, or String#end_with?
* | | Add `exists?` and `update_all` to `CollectionProxy` for respects an ↵Ryuta Kamizono2016-07-201-0/+20
| | | | | | | | | | | | | | | | | | association scope Fixes #25732.
* | | Merge pull request #25578 from ↵Rafael França2016-07-201-1/+1
|\ \ \ | | | | | | | | | | | | | | | | kamipo/move_warning_about_composite_primary_key_to_attribute_methods_primary_key Move the warning about composite primary key to `AttributeMethods::PrimaryKey`
| * | | Move the warning about composite primary key to `AttributeMethods::PrimaryKey`Ryuta Kamizono2016-07-021-1/+1
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Actually schema dumper/creation supports composite primary key (#21614). Therefore it should not show the warning about composite primary key in connection adapter. This change moves the warning to `AttributeMethods::PrimaryKey` and suppress the warning for habtm join table. Fixes #25388.
* / / Refactored test ↵bogdanvlviv2016-07-171-1/+2
|/ / | | | | | | `HasManyAssociationsTest#test_do_not_call_callbacks_for_delete_all`
* | Fix `calculate` with a string value in `from` and eager loadingRyuta Kamizono2016-06-131-0/+4
| | | | | | | | | | | | | | | | | | | | `construct_relation_for_association_calculations` pass a string value to `construct_join_dependency` when setting a string value in `from`. It should not pass a string value, but always `joins_values`. Related #14834, #19452. Fixes #24193.
* | Allow Oracle bind parameter syntax `:a1`Yasuo Honda2016-06-081-1/+1
| |
* | Exists shouldn't error when used with `includes`Sean Griffin2016-05-301-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently `exists?` does some hackery where it assumes that we can join onto anything that we passed to `eager_load` or `includes`, which doesn't work if we are joining onto a polymorphic association. Actually figuring out if we want to include something would require knowledge deep within the join dependency module, which is hard to pull up. The simplest solution is just to pass a flag down that says we're not actually going to try to eager load any of the data. It's not the solution I'd like, but that code really needs to be untangled before we can do much with it. This is another attempt at 6d5b1fd which should address the concerns that led to reverting it in 4ecabed.
* | Fix `has_one` `enum` `where` queriesJon Moss2016-05-261-0/+18
| | | | | | | | Fixes #25128
* | Support for unified Integer class in Ruby 2.4+Jeremy Daer2016-05-182-3/+3
| | | | | | | | | | | | | | | | Ruby 2.4 unifies Fixnum and Bignum into Integer: https://bugs.ruby-lang.org/issues/12005 * Forward compat with new unified Integer class in Ruby 2.4+. * Backward compat with separate Fixnum/Bignum in Ruby 2.2 & 2.3. * Drops needless Fixnum distinction in docs, preferring Integer.