aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/inverse_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Fix `CustomCops/AssertNot` to allow it to have failure messageRyuta Kamizono2018-05-131-2/+2
| | | | Follow up of #32605.
* Make `reflection.klass` raise if `polymorphic?` not to be misusedRyuta Kamizono2018-02-191-0/+10
| | | | | | | | | | | | | | | | This is an alternative of #31877 to fix #31876 caused by #28808. This issue was caused by a combination of several loose implementation. * finding automatic inverse association of polymorphic without context (caused by #28808) * returning `klass` even if `polymorphic?` (exists before #28808) * loose verification by `valid_inverse_reflection?` (exists before #28808) This makes `klass` raise if `polymorphic?` not to be misused. This issue will not happen unless polymorphic `klass` is misused. Fixes #31876. Closes #31877.
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-15/+15
|
* Merge pull request #31214 from ↵Eileen M. Uchitelle2017-11-291-0/+10
|\ | | | | | | | | chopraanmol1/bug_fix_has_one_inverse_owner_reload_from_validation Inverse instance should not be reloaded during autosave if called in validation
| * Inverse instance should not be reloaded during autosave if called in validationAnmol Chopra2017-11-271-0/+10
| | | | | | | | | | Record saved in save_has_one_association already make call to association.loaded! via record's before_save callback of save_belongs_to_association, but this will reload object if accessed in record's validation.
* | Provide arguments to RecordNotFoundNikita Misharin2017-11-251-1/+4
|/
* Load both `:authors` and `:author_addresses` to keep data integrityYasuo Honda2017-08-221-1/+1
| | | | | | | | | | | | | | | | | | | | `:authors` has a foreign key to `:author_addresses`. If only `:authors` fixture loaded into the database which supports foreign key and checks the existing data when enabling foreien keys like Oracle, it raises the following error `ORA-02298: cannot validate (ARUNIT.FK_RAILS_94423A17A3) - parent keys not found` It is because there is no parent data exists in `author_addresses` table. Here are how other database with foreign key support works: - MySQL does not check the existing data when enabling foreign key by `foreign_key_checks=1` https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_foreign_key_checks > Setting foreign_key_checks to 1 does not trigger a scan of the existing table data. Therefore, rows added to the table while foreign_key_checks=0 will not be verified for consistency. - PostgreSQL database itself has a feature to check existing data when enabling foreign key and discussed at #27636, which is reverted.
* Automatically guess the inverse associations for STIyui-knk2017-08-221-1/+38
| | | | | | | | | | | | | | | | | | | | | | ActiveRecord associations automatically guess the inverse associations. But this feature does not work correctly on assoctions for STI. For example, before this commit ``` class Post < ActiveRecord::Base belongs_to :author end class SpecialPost < Post; end class Author < ActiveRecord::Base has_many :posts has_many :special_posts end ``` `author.posts.first.author` works correctly, but `author.special_posts.first.author` does not work correctly.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Remove redundant `assert_respond_to`Ryuta Kamizono2017-07-031-26/+0
| | | | It is covered by following assertion.
* 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.
* Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-021-0/+1
|\ | | | | | | Enforce frozen string in Rubocop
| * Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| |
* | Merge pull request #28808 from fschwahn/fix-polymorphic-automic-inverseMatthew Draper2017-07-011-6/+2
|\ \ | |/ |/| Fix automatic inverse for polymorphic interfaces
| * Remove :polymorphic from INVALID_AUTOMATIC_INVERSE_OPTIONSFabian Schwahn2017-04-201-6/+2
| | | | | | | | | | | | | | This makes automatic inverse detection possible for polymorphic :has_one & :has_many possible. This resolves a number of issues, eg. `touch: true` on polymorphic relationships (#16446) and automatically setting inverse associations on newly built objects (#15028, #21843).
* | Remove a duplicate test of inverse_associations_test in ARKoichi ITO2017-05-211-14/+0
|/
* :warning: "Use assert_nil if expecting nil. This will fail in MT6."Akira Matsuda2017-01-181-1/+1
| | | | | | These are followups for 307065f959f2b34bdad16487bae906eb3bfeaf28, but TBH I'm personally not very much confortable with this style. Maybe we could override assert_equal in our test_helper not to warn?
* 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.
* Add three new rubocop rulesRafael Mendonça França2016-08-161-13/+13
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* Add `Style/EmptyLines` in `.rubocop.yml` and remove extra empty linesRyuta Kamizono2016-08-071-1/+0
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-25/+25
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-100/+100
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* remove args from assert_nothing_raised in testsTara Scherner de la Fuente2016-02-221-7/+7
|
* Tweaked wording used in some tests.Sebastian McKenzie2015-10-251-4/+4
|
* 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
* Fixed automatic inverse_of for models nested in moduleAndrew McCloud2014-06-151-0/+12
|
* Merge pull request #15343 from dontfidget/fix_polymorphic_automatic_inverse_ofRafael Mendonça França2014-06-131-0/+11
|\ | | | | prevent bad automatic inverse_of association
| * use name specified by 'as' for automatic inverse association to avoid ↵Andrew S. Brown2014-06-101-0/+11
| | | | | | | | reflecting on wrong association
* | Fix inverse associations testLeandro Facchinetti2014-06-041-1/+1
|/ | | | | | `InverseHasManyTests#test_parent_instance_should_be_shared_within_create_block_of_new_child` was mistakenly the same as `InverseHasManyTests#test_parent_instance_should_be_shared_within_build_block_of_new_child`.
* inversed instance should not be reloaded after stale state was changedDmitry Polushkin2013-10-131-0/+12
| | | check at association reader that record is inverted and should not be reloaded because of stale was changed at target record
* add regression test for set_inverse_instance on add_to_targetArthur Neves2013-10-041-0/+13
|
* Fixing multi-word automatic inverse detection.wangjohn2013-08-151-0/+14
| | | | | Currently, ActiveRecord models with multiple words cannot have their inverse associations detected automatically.
* using assert_not instead of refuteRajarshi Das2013-08-131-1/+1
|
* do not load all child records for inverse caseNeeraj Singh2013-06-211-0/+8
| | | | | | | | | | | | | | currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509
* fix bad test by making number that fits for integerNeeraj Singh2013-06-211-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | PR https://github.com/rails/rails/pull/10566 had to be reverted because after applying the fix test "test_raise_record_not_found_error_when_invalid_ids_are_passed" started failing. In this test invalid_id is being assigned a really large number which was causing following failure when PR #10566 was applied. ``` RangeError: bignum too big to convert into `long long' SELECT `interests`.* FROM `interests` WHERE `interests`.`man_id` = ? AND `interests`.`id` = ? LIMIT 1 [["man_id", 970345987], ["id", 2394823094892348920348523452345]] ``` This test is not failing in master because when test code `man.interests.find(invalid_id)` is executed then interests are fully loaded in memory and no database query is executed. After PR #10566 was merged then test code `man.interests.find(invalid_id)` started executing sql query and hence the error. In case someone is wondering why the second part of query is not failing, then that's because the actual query does not require any variable substituation where the number is large. In that case the sql generate is following. ``` SELECT `interests`.* FROM `interests` WHERE `interests`.`man_id` = ? AND `interests`.`id` IN (8432342, 2390102913, 2453245234523452) [["man_id", 970345987]] ```
* Revert "Merge pull request #10566 from neerajdotname/10509d"Jon Leighton2013-06-191-8/+0
| | | | | | | | | | This reverts commit 2b817a5e89ac0e7aeb894a40ae7151a0cf3cef16, reversing changes made to 353a398bee68c5ea99d76ac7601de0a5fef6f4a5. Conflicts: activerecord/CHANGELOG.md Reason: the build broke
* do not load all child records for inverse caseNeeraj Singh2013-06-191-0/+8
| | | | | | | | | | | | | | currently `post.comments.find(Comment.first.id)` would load all comments for the given post to set the inverse association. This has a huge performance penalty. Because if post has 100k records and all these 100k records would be loaded in memory even though the comment id was supplied. Fix is to use in-memory records only if loaded? is true. Otherwise load the records using full sql. Fixes #10509
* Created a method to automatically find inverse associations and cachewangjohn2013-05-071-0/+82
| | | | | | the results. Added tests to check to make sure that inverse associations are automatically found when has_many, has_one, or belongs_to associations are defined.
* Merge pull request #9996 from mikz/masterJon Leighton2013-04-051-0/+16
|\ | | | | 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/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | Updated the error message for +find+ on an inverse_of associationwangjohn2013-04-011-2/+0
| | | | | | | | | | so that it is consistent with the error thrown for +find+ without an inverse_of association.
* | Throwing a RecordNotFound exception when a record is scanned using thewangjohn2013-04-011-0/+18
|/ | | | | inverse_of option. I've also refactored the code for raising a RecordNotFound exception when searching for records with ids.
* Calling find() on an association with an inverse will now check to seewangjohn2013-03-191-0/+25
| | | | | if the association already holds that record in memory before checking the database for the specified ids.
* Fix issue with collection associations and first(n)/last(n)Carlos Antonio da Silva2012-11-011-0/+13
| | | | | | | | | | | | | | | | | | | | | | | When calling first(n) or last(n) in a collection, Active Record was improperly trying to set the inverse of instance in case that option existed. This change was introduced by fdf4eae506fa9895e831f569bed3c4aa6a999a22. In such cases we don't need to do that "manually", since the way collection will be loaded will already handle that, so we just skip setting the inverse association when any argument is given to first(n)/last(n). The test included ensures that these scenarios will have the inverse of instance set properly. Fixes #8087, Closes #8094. Squashed cherry-pick from d37d40b and c368b66. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/collection_association.rb
* Use inversed parent for first and last child of has_many associationbrainopia2012-08-181-0/+6
|
* Deprecate ActiveRecord::Base.scoped.Jon Leighton2012-07-271-9/+9
| | | | | | | It doesn't serve much purpose now that ActiveRecord::Base.all returns a Relation. The code is moved to active_record_deprecated_finders.
* find and replace deprecated keysJon Leighton2012-04-271-9/+9
|
* %s/find(:\(first\|last\|all\), \([^()]*\))/scoped(\2).\1/gcI amongst other ↵Jon Leighton2012-04-271-9/+9
| | | | things
* remove calls to find(:first), find(:last) and find(:all)Jon Leighton2012-04-261-18/+18
|
* Revert "Deprecate implicit eager loading. Closes #950."Jon Leighton2012-01-161-4/+4
| | | | This reverts commit c99d507fccca2e9e4d12e49b4387e007c5481ae9.