aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
Commit message (Collapse)AuthorAgeFilesLines
* remove deprecated implicit join references.Yves Senn2013-06-291-15/+5
|
* Apply default scope when joining associations.Jon Leighton2013-06-281-0/+8
| | | | | | | | | | | | | | | | | | | For example: class Post < ActiveRecord::Base default_scope -> { where published: true } end class Comment belongs_to :post end When calling `Comment.join(:post)`, we expect to receive only comments on published posts, since that is the default scope for posts. Before this change, the default scope from `Post` was not applied, so we'd get comments on unpublished posts.
* Remove depreacted findersŁukasz Strzałkowski2013-06-281-2/+2
| | | | They were deprecated in 4.0, planned to remove in 4.1
* Fix `another_contract` not being used warningVipul A M2013-06-241-1/+1
|
* test-case to prevent regressions described in #10901.Jared Armstrong2013-06-241-0/+27
|
* test-case to prevent regressions on `Association#build` with an Array.Yves Senn2013-06-221-0/+10
| | | | Closes #11026
* 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
* Merge pull request #10987 from senny/10979_association_include_returns_trueXavier Noria2013-06-181-11/+11
|\ | | | | `CollectionProxy#include?` returns `true` and `false` as documented.
| * `CollectionProxy#include?` returns `true` and `false` as documented.Yves Senn2013-06-181-11/+11
| |
* | Merge pull request #10533 from vipulnsward/fix_testRafael Mendonça França2013-06-151-1/+2
|\ \ | |/ |/| Fix test, addresss => address
| * Make test name descriptive and add reference to original regression commitVipul A M2013-06-151-1/+2
| |
* | Merge pull request #10824 from vipulnsward/wonderfulGuillermo Iguaran2013-06-021-1/+1
|\ \ | | | | | | wonderfull => wonderful
| * | wonderfull => wonderfulVipul A M2013-06-021-1/+1
| | |
* | | Remove #sum with a block was deprecated.kennyj2013-06-011-6/+0
|/ /
* | `implicit_readonly` is being removed in favor of calling `readonly` explicitlyYves Senn2013-05-271-4/+4
| |
* | Fix the `:primary_key` option for `has_many` associations.Yves Senn2013-05-231-0/+8
|/ | | | | | | | | | | | | | | | | When removing records from a `has_many` association it used the `primary_key` defined on the association. Our test suite didn't fail because on all occurences of `:primary_key`, the specified column was available in both tables. This prevented the code from raising an exception but it still behaved badly. I added a test-case to prevent regressions that failed with: ``` 1) Error: HasManyAssociationsTest#test_has_many_assignment_with_custom_primary_key: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: essays.first_name: UPDATE "essays" SET "writer_id" = NULL WHERE "essays"."writer_id" = ? AND "essays"."first_name" IS NULL ```
* 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.
* Confirm a record has not already been destroyed before decrementingBen Tucker2013-05-061-0/+20
| | | | | | | | | counter cache At present, calling destroy multiple times on the same record results in the belongs_to counter cache being decremented multiple times. With this change the record is checked for whether it is already destroyed prior to decrementing the counter cache.
* Fix #8856 Ensure has_one association=(associate) triggers save.Chris Thompson2013-04-301-0/+16
| | | | | | | | | | | | | | | | | | | | | | | activerecord/lib/active_record/associations.rb states: # [association=(associate)] # Assigns the associate object, extracts the primary key, sets it as the foreign key, # and saves the associate object. Since commit 42dd5d9f2976677a4bf22347f2dde1a8135dfbb4 to fix #7191, this is no longer the case if the associate has changed, but is the same object. For example: # Pirate has_one :ship pirate = Pirate.create!(catchphrase: "A Pirate") ship = pirate.build_ship(name: 'old name') ship.save! ship.name = 'new name' pirate.ship = ship That last line should trigger a save. Although we are not changing the association, the associate (ship) has changed.
* Update counter cache when pushing into associationMatthew Robertson2013-04-211-0/+9
| | | | | | | | | | | | | | | | This commit fixes a regression bug in which counter_cache columns were not being updated correctly when newly created records were being pushed into an assocation. EG: # this was fine @post.comment.create! # this was fine @comment = Comment.first @post.comments << @comment # this would not update counters @post.comments << Comment.create!
* 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
* | has_many through obeys order on through associationNeeraj Singh2013-04-042-2/+8
| | | | | | | | fixes #10016
* | 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.
* test-case to prevent resetting associations when preloading twice.Yves Senn2013-03-221-0/+7
| | | | | | | Conflicts: activerecord/lib/active_record/associations/preloader/through_association.rb activerecord/test/cases/associations/eager_test.rb
* Fix some typos in AR- CHANGELOG, tests, method doc. fixedVipul A M2013-03-201-1/+1
|
* 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.
* Merge pull request #9782 from vipulnsward/change_from_each_to_each_key_habtmRafael Mendonça França2013-03-181-2/+2
|\ | | | | Change from each to each_value;drop assignment in habtm
| * 1. Change from each to each_value since we did not use keyVipul A M2013-03-191-2/+2
| | | | | | | | 2. drop assignment of value to sum in test
* | fix typos in AR. lots of them.Vipul A M2013-03-191-1/+1
|/
* If a counter_cache is defined, then using update_attributes and changingJohn Wang2013-03-151-0/+31
| | | | | the primary key on an association will make sure that the corresponding counter on the association is changed properly. Fixes #9722.
* Deprecate the `:distinct` option for `Relation#count`.Yves Senn2013-03-152-2/+2
| | | | | | | We moved more and more away from passing options to finder / calculation methods. The `:distinct` option in `#count` was one of the remaining places. Since we can now combine `Relation#distinct` with `Relation#count` the option is no longer necessary and can be deprecated.
* rename `Relation#uniq` to `Relation#distinct`. `#uniq` still works.Yves Senn2013-03-153-4/+4
| | | | | | | | The similarity of `Relation#uniq` to `Array#uniq` is confusing. Since our Relation API is close to SQL terms I renamed `#uniq` to `#distinct`. There is no deprecation. `#uniq` and `#uniq!` are aliases and will continue to work. I also updated the documentation to promote the use of `#distinct`.
* Refactored test case with standard variable namingAnupam Choudhury2013-03-081-40/+40
|
* Refactored and removed unnecessary lines in the test caseAnupam Choudhury2013-03-071-3/+3
|
* Expand order(:symbol) to "table".symbol to prevent broken queries on PG.Yves Senn2013-02-251-1/+6
| | | | | | | | | | Fixes #9275. When `#order` is called with a Symbol this patch will prepend the quoted_table_name. Before the postgresql adapter failed to build queries containg a join and an order with a symbol. This expansion happens for all adapters.
* Remove warningRafael Mendonça França2013-02-241-1/+1
|
* don't apply invalid ordering when preloading hmt associations.Yves Senn2013-02-241-0/+5
| | | | | | | | | | | | | | | closes #8663. When preloading a hmt association there two possible scenarios: 1.) preload with 2 queries: first hm association, then hmt with id IN () 2.) preload with join: hmt association is loaded with a join on the hm association The bug was happening in scenario 1.) with a normal order clause on the hmt association. The ordering was also applied when loading the hm association, which resulted in the error. This patch only applies the ordering the the hm-relation if we are performing a join (2). Otherwise the order will only appear in the second query (1).
* test case to prevent duplicated associations with custom PK.Yves Senn2013-02-241-0/+10
| | | | closes #9201
* don't cache invalid subsets when preloading hmt associations.Yves Senn2013-02-141-0/+6
| | | | closes #8423.
* Prevent Relation#merge from collapsing wheres on the RHSJon Leighton2013-01-271-0/+7
| | | | | | | | | | | | | | | | | | | | | | | This caused a bug with the new associations implementation, because now association conditions are represented as Arel nodes internally right up to when the whole thing gets turned to SQL. In Rails 3.2, association conditions get turned to raw SQL early on, which prevents Relation#merge from interfering. The current implementation was buggy when a default_scope existed on the target model, since we would basically end up doing: default_scope.merge(association_scope) If default_scope contained a where(foo: 'a') and association_scope contained a where(foo: 'b').where(foo: 'c') then the merger would see that the same column is representated on both sides of the merge and collapse the wheres to all but the last: where(foo: 'c') Now, the RHS of the merge is left alone. Fixes #8990
* Fix cases where delete_records on a has_many association caused errorsDerek Kraan2013-01-271-1/+19
| | | | | | | | | | | | | because of an ambiguous column name. This happened if the association model had a default scope that referenced a third table, and the third table also referenced the original table (with an identical foreign_key). Mysql requires that ambiguous columns are deambiguated by using the full table.column syntax. Postgresql and Sqlite use a different syntax for updates altogether (and don't tolerate table.name syntax), so the fix requires always including the full table.column and discarding it later for Sqlite and Postgresql.
* Remove obsolete test fileJon Leighton2013-01-251-35/+0
| | | | https://github.com/rails/rails/commit/4beb4dececcf10c642c74fbcb8548c833e921a86#commitcomment-2482869
* A test case name needs to start with "test_"Akira Matsuda2013-01-231-1/+1
|
* Preload some join table schemasAkira Matsuda2013-01-221-0/+9
|
* Revert "Merge pull request #8989 from robertomiranda/use-rails-4-find-by"Guillermo Iguaran2013-01-186-21/+21
| | | | | This reverts commit 637a7d9d357a0f3f725b0548282ca8c5e7d4af4a, reversing changes made to 5937bd02dee112646469848d7fe8a8bfcef5b4c1.