aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/belongs_to_associations_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #10489 from greenriver/ar_counter_cache_multiple_destroyRafael Mendonça França2013-05-061-0/+20
| | | | | | | | | | | Confirm a record has not already been destroyed before decrementing counter cache Conflicts: activerecord/CHANGELOG.md Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/belongs_to.rb
* Fix issue #7526. Reload the target if it's stale.larrylv2013-03-051-0/+14
| | | | | | | | * This has been fixed at master via `365b8b6`, but not at 3-2-stable branch. * @stale_state should be nil when a model isn't saved. via `0f3901e`. * set @stale_state to nil when reset the target.
* Fix undefined method `to_i' introduced since 3.2.8Jason Stirk2013-01-041-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit fixes a bug introduced in 96a13fc7 which breaks behaviour of integer fields in 3.2.8. 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)
* ConnectionAdapters::Column.type_cast_code should always convert values to ↵Thiago Pradi2012-09-091-0/+6
| | | | integer calling #to_i
* Fix belongs_to polymorphic with custom primary key on target.Jon Leighton2011-09-261-0/+8
| | | | Closes #3104.
* Don't find belongs_to target when the foreign_key is NULL. Fixes #2828Georg Friedrich2011-09-051-0/+6
|
* Fix exception if old and new targets are both nil. Fixes #1471.Jon Leighton2011-07-121-0/+9
|
* Ensure that the foreign key gets set when doing record.create_association or ↵Jon Leighton2011-07-081-0/+14
| | | | record.create_association. Fixes #1960.
* added more tests for update_columnganesh2011-06-041-0/+9
|
* Handle polymorphic_type NOT NULL-able columns as well.thedarkone2011-05-211-0/+11
|
* Add block setting of attributes to singular associationsAndrew White2011-05-171-0/+21
|
* assert_difference can take a callable piece of code rather than just evaling ↵Aaron Patterson2011-05-011-4/+4
| | | | a string
* fixing more test warnings in 1.9.3Aaron Patterson2011-04-301-3/+3
|
* Split AssociationProxy into an Association class (and subclasses) which ↵Jon Leighton2011-02-181-8/+5
| | | | manages the association, and a CollectionProxy class which is *only* a proxy. Singular associations no longer have a proxy. See CHANGELOG for more.
* belongs_to records should be initialized within the association scopeJon Leighton2011-01-161-0/+21
|
* Add create_association! for belongs_toJon Leighton2011-01-161-0/+17
|
* Fix polymorphic belongs_to associationproxy raising errors when loading target.Ernie Miller2011-01-111-0/+9
|
* just use a hash for doing association cachingAaron Patterson2011-01-071-2/+2
|
* fixing merge errorsAaron Patterson2011-01-041-11/+0
|
* Allow assignment on has_one :through where the owner is a new record [#5137 ↵Jon Leighton2011-01-031-19/+14
| | | | | | | | | | state:resolved] This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct. Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things. Phew, long commit message!
* Some basic tests for the :foreign_type option on belongs_to, which was ↵Jon Leighton2011-01-031-0/+19
| | | | previously completely untested.
* Support for :counter_cache on polymorphic belongs_toJon Leighton2010-12-311-0/+12
|
* Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociation to ↵Jon Leighton2010-12-311-6/+36
| | | | inherit from it
* If a has_many goes :through a belongs_to, and the foreign key of the ↵Jon Leighton2010-12-231-23/+37
| | | | belongs_to changes, then the has_many should be considered stale.
* Setting the id of a belongs_to object updates all referenced objects [#2989 ↵Jeff Dean2010-12-081-0/+37
| | | | state:resolved]
* use persisted? instead of new_record? wherever possibleDavid Chelimsky2010-11-091-6/+6
| | | | | | | | | | | - persisted? is the API defined in ActiveModel - makes it easier for extension libraries to conform to ActiveModel APIs without concern for whether the extended object is specifically ActiveRecord [#5927 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
* Convert :primary_key in association to a string before comparing to column ↵Denis Odorcic2010-10-301-0/+7
| | | | names, so that for example :primary_key => :another_pk works as well [#5605 state:resolved]
* Set attributes properly for model built from association with conditions ↵Marcelo Giorgi2010-09-281-0/+5
| | | | | | [#5562 state:resolved] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
* removing unused codeAaron Patterson2010-08-221-2/+2
|
* code gardening: we have assert_(nil|blank|present), more concise, with ↵Xavier Noria2010-08-171-1/+1
| | | | better default failure messages - let's use them
* update tests for mysql2 supportBrian Lopez2010-08-021-1/+1
|
* Don't increment and then decrement the same counter when re-assigning a ↵Tarmo Tänav2010-07-291-0/+4
| | | | | | | | belongs_to association [#2786 state:committed] Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
* removing unused models from testsSubba Rao Pasupuleti2010-07-211-2/+0
| | | | | | [#5153 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
* fix test_belongs_to_with_primary_key_joins_on_correct_column test on OracleRaimonds Simanovskis2010-06-041-0/+4
|
* Use better assertion methods for testingNeeraj Singh2010-05-191-2/+2
| | | | | | [#4645 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
* kill warnings on 1.8.7 [#4331 state:resolved]Aaron Patterson2010-04-101-4/+4
| | | | Signed-off-by: José Valim <jose.valim@gmail.com>
* Fix failing test in MySQL.Emilio Tagua2010-03-301-3/+8
| | | | Signed-off-by: José Valim <jose.valim@gmail.com>
* Fix honoring :primary_key option when joining or eager loading a belongs_to ↵Ernie Miller2010-03-291-0/+13
| | | | | | | | association [#765 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
* Add :dependent = to has_one and has_many [#3075 state:resolved]Rizwan Reza2010-03-281-1/+7
|
* Fix associations to call :destroy or :delete based on the right :dependent ↵Carlos Antonio da Silva2010-03-091-6/+26
| | | | | | option Signed-off-by: José Valim <jose.valim@gmail.com>
* Removed duplicated tests [#3026 state:resolved]Arthur Zapparoli2009-08-101-18/+0
| | | | Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
* added :order option to find :first methods and associations as otherwise ↵Raimonds Simanovskis2009-08-061-2/+4
| | | | | | | | Oracle tests were failing Oracle stores '' string as NULL Oracle cannot have identifiers larger than 30 characters added missing fixtures to test setup method
* Add primary_key option to belongs_to associationSzymon Nowak2009-07-151-0/+98
| | | | | | [#765 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
* Fixed autosave checks on objects with hm:t in :include [#2213 state:resolved]Will Bryant2009-03-121-0/+16
| | | | Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
* Ruby 1.9 compat: rename deprecated assert_raises to assert_raise.Jeremy Kemper2009-03-081-2/+2
| | | | [#1617 state:resolved]
* Ensure belongs_to association with a counter cache in name spaced model ↵Adam Cooper2009-03-061-0/+17
| | | | | | works [#1678 state:resolved] Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
* Fixed that autosave should validate associations even if master is invalid ↵David Heinemeier Hansson2009-02-271-129/+0
| | | | [#1930 status:committed]
* Ensure methods called on association proxies respect access control. [#1083 ↵Pratik Naik2008-10-131-0/+10
| | | | state:resolved] [Adam Milligan, Pratik]
* Remove the functionality introduce in 28d3390Michael Koziarski2008-10-101-13/+0
| | | | There are several situations it doesn't cater for, and the inconsistency isn't worth blocking 2.2.
* Support for updating a belongs to association from the foreign key (without ↵Jon Leighton2008-09-131-0/+13
| | | | | | | saving and reloading the record) Signed-off-by: Michael Koziarski <michael@koziarski.com> [#142 state:committed]