aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations
Commit message (Collapse)AuthorAgeFilesLines
* 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.
* Merge pull request #8989 from robertomiranda/use-rails-4-find-byGuillermo Iguaran2013-01-186-21/+21
|\ | | | | Replace deprecated find_by_* with find_by
| * User Rails 4 find_byrobertomiranda2013-01-186-21/+21
| |
* | Undeprecate the :extend optionJon Leighton2013-01-181-0/+12
| | | | | | | | | | | | | | Suggested by @dhh. It doesn't affect the generated SQL, so seems reasonable to continue to allow it as an association option.
* | CollectionProxy should be default scopedJon Leighton2013-01-181-0/+5
| | | | | | | | Fixes #8795
* | `CollectionAssociation#empty?` respects newly builded recordsYves Senn2013-01-131-0/+7
|/
* Merge pull request #8568 from inossidabile/fix-in_clause_lengthJon Leighton2013-01-111-7/+7
|\ | | | | Correct source for in_clause_length for eager loading (Fix for #8474)
| * Eager loading made to use relation's in_clause_length instead of host's one ↵Boris Staal2012-12-201-7/+7
| | | | | | | | (fixes #8474)
* | deprecate `assert_blank` and `assert_present`.Yves Senn2013-01-051-2/+2
| | | | | | | | | | They don't add any benefits over `assert object.blank?` and `assert object.present?`
* | Fix undefined method `to_i' introduced since 3.2.8Jason Stirk2013-01-041-0/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes a bug introduced in 96a13fc7 which breaks behaviour of integer fields. In 3.2.8, setting the value of an integer field to a non-integer (eg. Array, Hash, etc.) would default to 1 (true) : # 3.2.8 p = Post.new p.category_id = [ 1, 2 ] p.category_id # => 1 p.category_id = { 3 => 4 } p.category_id # => 1 In 3.2.9 and above, this will raise a NoMethodError : # 3.2.9 p = Post.new p.category_id = [ 1, 2 ] NoMethodError: undefined method `to_i' for [1, 2]:Array Whilst at first blush this appear to be sensible, it combines in bad ways with scoping. For example, it is common to use scopes to control access to data : @collection = Posts.where(:category_id => [ 1, 2 ]) @new_post = @collection.new In 3.2.8, this would work as expected, creating a new Post object (albeit with @new_post.category_id = 1). However, in 3.2.9 this will cause the NoMethodError to be raised as above. It is difficult to avoid triggering this error without descoping before calling .new, breaking any apps running on 3.2.8 that rely on this behaviour. This patch deviates from 3.2.8 in that it does not retain the somewhat spurious behaviour of setting the attribute to 1. Instead, it explicitly sets these invalid values to nil : p = Post.new p.category_id = [ 1, 2 ] p.category_id # => nil This also fixes the situation where a scope using an array will "pollute" any newly instantiated records. @new_post = @collection.new @new_post.category_id # => nil Finally, 3.2.8 exhibited a behaviour where setting an object to an integer field caused it to be coerced to "1". This has not been retained, as it is spurious and surprising in the same way that setting Arrays and Heshes was : c = Category.find(6) p = Post.new # 3.2.8 p.category_id = c p.category_id # => 1 # This patch p.category_id = c p.category_id # => nil This commit includes explicit test cases that expose the original issue with calling new on a scope that uses an Array. As this is a common situation, an explicit test case is the best way to prevent regressions in the future. It also updates and separates existing tests to be explicit about the situation that is being tested (eg. AR objects vs. other objects vs. non-integers)
* | Rename update_attributes method to update, keep update_attributes as an aliasAmparo Luna + Guillermo Iguaran2013-01-033-8/+9
| |
* | Standardize the use of current_adapter?Rafael Mendonça França2013-01-011-1/+1
|/
* Fix for has_many_through counter_cache bugMatthew Robertson2012-12-141-0/+11
| | | | | | This commit fixes reported issue #7630 in which counter caches were not being updated properly when replacing has_many_through relationships
* Move where with blank conditions test to the correct where tests fileCarlos Antonio da Silva2012-12-071-6/+0
| | | | This test does not belong to has many associations test.
* Ensure there won't be any regression with where(nil) callsCarlos Antonio da Silva2012-12-071-1/+1
| | | | | | | | | | | | | | | | | | | Consider this scenario: if params[:foo] conditions = { foo: true } end foos = Foo.where(conditions).order(:id) When params[:foo] is nil, this would call: foos = Foo.where(nil).order(:id) In this scenario, we want Foo.where(conditions) to be the same as calling Foo.all, otherwise we'd get a "NoMethodError order for WhereChain". Related to #8332.
* Merge pull request #8332 from amatsuda/ar_where_chainCarlos Antonio da Silva2012-12-071-1/+1
|\ | | | | | | | | | | | | | | Relation.where with no args can be chained with not, like, and not_like Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/relation/query_methods.rb
| * Relation.where with no args can be chained with not, like, and not_likeAkira Matsuda2012-11-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | examples: Model.where.not field: nil #=> "SELECT * FROM models WHERE field IS NOT NULL Model.where.like name: 'Jeremy%' #=> "SELECT * FROM models WHERE name LIKE 'Jeremy%' this feature was originally suggested by Jeremy Kemper https://github.com/rails/rails/pull/5950#issuecomment-5591330 Closes #5950
* | Do not instantiate intermediate AR objects when eager loading.Yves Senn2012-12-041-0/+6
| | | | | | | | Closes #3313
* | Remove not used variable warnignsCarlos Antonio da Silva2012-12-011-4/+4
| |
* | User assert_kind_of, invert assert_equal expectationsCarlos Antonio da Silva2012-11-293-9/+9
| |
* | Added STI support to init and building associationsJason Rush2012-11-293-0/+86
|/ | | | | | | | Allows you to do BaseClass.new(:type => "SubClass") as well as parent.children.build(:type => "SubClass") or parent.build_child to initialize an STI subclass. Ensures that the class name is a valid class and that it is in the ancestors of the super class that the association is expecting.
* Corrects typo in test nameAndy Lindeman2012-11-261-1/+1
|
* Merge pull request #8291 from senny/8265_build_with_polymorphic_associationRafael Mendonça França2012-11-221-0/+8
|\ | | | | | | | | | | | | prevent mass assignment of polymorphic type when using `build` Conflicts: activerecord/CHANGELOG.md
| * prevent mass assignment of polymorphic type when using `build`Yves Senn2012-11-221-0/+8
| | | | | | | | Closes #8265
* | Deprecate Relation#sum with a block.Carlos Antonio da Silva2012-11-211-0/+6
|/ | | | | | | To perform a sum calculation over the array of elements, use to_a.sum(&block). Please check the discussion in f9cb645dfcb5cc89f59d2f8b58a019486c828c73 for more context.
* Test for has_many bug on unsaved recordsGeorge Brocklehurst2012-11-161-0/+7
| | | | See issue #7950.
* Merge and add tests related to 5215Vipul A M2012-11-123-18/+25
|
* Do not create useless database transaction when building `has_one` association.Bogdan Gusiev2012-11-101-0/+6
|
* Merge pull request #8116 from senny/7993_configure_counter_cache_for_has_manyJon Leighton2012-11-091-0/+8
|\ | | | | :counter_cache option for to support custom named counter caches
| * :counter_cache option for to support custom named counter caches. Closes #7993Yves Senn2012-11-041-0/+8
| |
* | Delegate all calculations to the scope.Jon Leighton2012-11-091-0/+1
| | | | | | | | | | | | | | So that the scope may be a NullRelation and return a result without executing a query. Fixes #7928
* | CollectionProxy#pluck issues no query for a new_record? ownerJon Leighton2012-11-091-0/+1
| | | | | | | | | | | | | | Fixes #8102. I couldn't find a nicer way to deal with this than delegate the call to #scope, which will be a NullRelation when we want it to be.
* | Relations built off collection associations with an unsaved owner should be ↵Jon Leighton2012-11-091-0/+9
|/ | | | | | | | | | null relations For example, the following should not run any query on the database: Post.new.comments.where(body: 'omg').to_a # => [] Fixes #5215.
* 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
* Merge pull request #7887 from senny/remove_unused_requires_in_ar_testsVijay Dev2012-10-101-1/+0
|\ | | | | remove duplicated require statements in AR test cases
| * remove duplicated require statements in AR test casesYves Senn2012-10-091-1/+0
| |
* | Fix has_many assocation w/select load after createErnie Miller2012-10-051-0/+8
|/ | | | | | | | | | | | | | If you create a new record via a collection association proxy that has not loaded its target, and which selects additional attributes through the association, then when the proxy loads its target, it will inadvertently trigger an ActiveModel::MissingAttributeError during attribute writing when CollectionAssociation#merge_target_lists attempts to do its thing, since the newly loaded records will possess attributes the created record does not. This error also raises a bogus/confusing deprecation warning when accessing the association in Rails 3.2.x, so cherry-pick would be appreciated!
* Revert "Use flat_map { } instead of map {}.flatten"Santiago Pastorino2012-10-052-2/+2
| | | | | | | | | | | This reverts commit abf8de85519141496a6773310964ec03f6106f3f. We should take a deeper look to those cases flat_map doesn't do deep flattening. irb(main):002:0> [[[1,3], [1,2]]].map{|i| i}.flatten => [1, 3, 1, 2] irb(main):003:0> [[[1,3], [1,2]]].flat_map{|i| i} => [[1, 3], [1, 2]]
* Use flat_map { } instead of map {}.flattenSantiago Pastorino2012-10-052-2/+2
|