Aaron Patterson
3f4143eedb
fixing merge errors
2011-01-04 15:16:56 -08:00
Jon Leighton
40afcade0d
Remove undocumented feature from has_one where you could pass false as the second parameter to build_assoc or create_assoc, and the existing associated object would be untouched (the foreign key would not be nullified, and it would not be deleted). If you want behaviour similar to this you can do the following things:
...
* Use :dependent => :nullify (or don't specify :dependent) if you want to prevent the existing associated object from being deleted
* Use has_many if you actually want multiple associated objects
* Explicitly set the foreign key if, for some reason, you really need to have multiple objects associated with the same has_one. E.g.
previous = obj.assoc
obj.create_assoc
previous.update_attributes(:obj_id => obj.id)
2011-01-03 16:24:32 -08:00
Jon Leighton
2120da7f73
ActiveRecord::Relation#primary_key should return a string, just like ActiveRecord::Base.primary_key does.
2011-01-03 16:24:32 -08:00
Jon Leighton
0619dc2319
Implement deprecated version of AssociationReflection#primary_key_name, which has been renamed to #foreign_key. Also bumping the deprecation_horizon in Active Support to 3.1.
2011-01-03 16:24:32 -08:00
Jon Leighton
a9bed985cf
When preloading a belongs_to, the target should still be set (to nil) if there is no foreign key present. And the loaded flag should be set on the association proxy. This then allows us to remove the foreign_key_present? check from BelongsToAssociation#find_target. Also added a test for the same thing on polymorphic associations.
2011-01-03 16:24:32 -08:00
Jon Leighton
1313d386da
Make Relation#create_with always merge rather than overwrite, not just when merging two relations. If you wish to overwrite, you can do relation.create_with(nil), or for a specific attribute, relation.create_with(:attr => nil).
2011-01-03 16:24:32 -08:00
Jon Leighton
3103296a61
Let AssociationCollection#find use #scoped to do its finding. Note that I am removing test_polymorphic_has_many_going_through_join_model_with_disabled_include, since this specifies different behaviour for an association than for a regular scope. It seems reasonable to expect scopes and association proxies to behave in roughly the same way rather than having subtle differences.
2011-01-03 16:24:31 -08:00
Jon Leighton
d6289aadce
Fix test_any in relations_test.rb, which was failing when relations_test.rb is run on its own (it passes when the entire suite is run). This is a hacky fix for a problem I didn't quite get to the bottom of, so I'd welcome a better solution...
2011-01-03 16:24:31 -08:00
Jon Leighton
a0be389d39
Allow assignment on has_one :through where the owner is a new record [ #5137 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!
2011-01-03 16:24:31 -08:00
Jon Leighton
c47f802d0e
Have a proper AssociationReflection#foreign_type method rather than using options[:foreign_type]
2011-01-03 16:24:31 -08:00
Jon Leighton
16065b4f19
Some basic tests for the :foreign_type option on belongs_to, which was previously completely untested.
2011-01-03 16:24:30 -08:00
Aaron Patterson
60cf65def8
herp derpricating add_limit_offset!
2011-01-03 15:59:13 -08:00
Robert Pankowecki (Gavdi)
febdf5a5a9
Added one more failing test for bug #6036
2011-01-04 01:21:25 +08:00
Jon Leighton
1267598881
Rename AssociationReflection#primary_key_name to foreign_key, since the options key which it relates to is :foreign_key
2010-12-31 20:00:45 +00:00
Jon Leighton
3c400627eb
Support for :counter_cache on polymorphic belongs_to
2010-12-31 20:00:44 +00:00
Jon Leighton
bea4065d3c
Refactor BelongsToAssociation to allow BelongsToPolymorphicAssociation to inherit from it
2010-12-31 20:00:44 +00:00
Jon Leighton
62b084f807
Specify the STI type condition using SQL IN rather than a whole load of ORs. Required a fix to ActiveRecord::Relation#merge for properly merging create_with_value. This also fixes a situation where the type condition was appearing twice in the resultant SQL query.
2010-12-31 20:00:44 +00:00
Jon Leighton
2bf3186803
Verify that when has_many associated objects are destroyed via :dependent => :destroy, when the parent is destroyed, the callbacks are run
2010-12-31 20:00:44 +00:00
Jon Leighton
38fbfa6390
Refactor configure_dependency_for_has_many to use AssociationCollection#delete_all. It was necessary to change test_before_destroy in lifecycle_test.rb so that it checks topic.replies.size *before* doing the destroy, as afterwards it will now (correctly) be 0.
2010-12-31 20:00:44 +00:00
Pratik Naik
573fd39e22
Make sure Model#touch doesn't try to update non existing columns
2010-12-30 18:42:18 +00:00
Pratik Naik
fd1cf13f74
Make serialized fixtures work again
2010-12-29 16:15:45 +00:00
Santiago Pastorino
304d38c053
Allow primary_key to be an attribute when the model is a new record
2010-12-28 10:11:27 -02:00
Aaron Patterson
7e91ad3f89
stop calling deprecated apis
2010-12-26 19:56:18 -07:00
Jon Leighton
e8ada11aac
Associations: DRY up the code which is generating conditions, and make it all use arel rather than SQL strings
2010-12-26 19:38:04 +00:00
Jon Leighton
f2230c06ed
Fix dodgy tests which were effectively asserting nil == nil
2010-12-26 19:38:04 +00:00
Aaron Patterson
5b918bb97c
using arel to compile sql statements
2010-12-25 16:19:59 -07:00
Aaron Patterson
a6fe244e9b
take more advantage of arel sql compiler
2010-12-25 15:23:45 -07:00
Aaron Patterson
ec13305b21
stop redifining methods on every call to set_primary_key
2010-12-24 22:15:41 -07:00
oleg dashevskii
6974c595fd
Verify that there is no unwanted implicit readonly set on Model.has_many_through.find(id) [ #5442 state:resolved]
2010-12-23 15:19:18 -08:00
Jon Leighton
fb3a8c51b4
Raise an error for associations which try to go :through a polymorphic association [ #6212 state:resolved]
2010-12-23 15:19:18 -08:00
Jon Leighton
1c07b84df9
If a has_many goes :through a belongs_to, and the foreign key of the belongs_to changes, then the has_many should be considered stale.
2010-12-23 15:19:18 -08:00
Jon Leighton
2d9626fc74
Improved strategy for updating a belongs_to association when the foreign key changes. Rather than resetting each affected association when the foreign key changes, we should lazily check for 'staleness' (where fk does not match target id) when the association is accessed.
2010-12-23 15:19:18 -08:00
Jon Leighton
3f17ed407c
Test to verify that #2189 (count with has_many :through and a named_scope) is fixed
2010-12-23 15:19:18 -08:00
Jon Leighton
1619c2435b
Revert "Optimize <association>_ids for hm:t with belongs_to source". The optimisation has too many edge cases, such as when the reflection, source reflection, or through reflection has conditions, orders, etc. [ #6153 state:resolved]
...
This reverts commit 373b053dc8 .
Conflicts:
activerecord/lib/active_record/associations.rb
2010-12-23 15:19:18 -08:00
Jon Leighton
4e13625818
Test demonstrating problem with foo.association_ids where it's a has_many :through with :conditions, with a belongs_to as the source reflection
2010-12-23 15:19:18 -08:00
Jon Leighton
ff7bde62c8
When a has_many association is not :uniq, appending the same record multiple times should append it to the @target multiple times [ #5964 state:resolved]
2010-12-23 15:19:17 -08:00
Michał Łomnicki
030480ac1f
Fix behaviour of foo.has_many_through_association.select('custom select') [ #6089 state:resolved]
2010-12-23 15:19:17 -08:00
Szymon Nowak
85683f2a79
Fix creation of has_many through records with custom primary_key option on belongs_to [ #2990 state:resolved]
2010-12-23 15:19:17 -08:00
Will Bryant
b79823832e
Verify that has_one :through preload respects the :conditions [ #2976 state:resolved]
2010-12-23 15:19:17 -08:00
Jon Leighton
c6db37e69b
Don't allow a has_one association to go :through a collection association [ #2976 state:resolved]
2010-12-23 15:19:17 -08:00
Raimonds Simanovskis
d9c8c47e3d
Fix for default_scope tests to ensure comparing of equally sorted lists
...
This is additional fix for commit
ebc47465a5 Respect the default_scope on a join model when reading a through association
which otherwise was failing on Oracle (as it returned fixture comments in different order).
2010-12-23 23:19:59 +08:00
Aaron Patterson
3e64336647
removing SQL interpolation, please use scoping and attribute conditionals as a replacement
2010-12-22 18:23:38 -08:00
Aaron Patterson
207f266cca
define_attr_method must serialize nil correctly
2010-12-20 19:47:15 -08:00
Aaron Patterson
099a210c83
if there is no base name, we cannot determine a primary key
2010-12-20 19:47:15 -08:00
Jon Leighton
834e5336a5
has_many associations with :dependent => :delete_all should update the counter cache when deleting records
2010-12-20 13:56:04 -08:00
Jon Leighton
b8153fd5a1
Fix problem where wrong keys are used in JoinAssociation when an association goes :through a belongs_to [ #2801 state:resolved]
2010-12-20 13:56:04 -08:00
Piotr Sarnacki
40b15f9f38
ActiveRecord::Base.joins should allow single nil argument [ #6181 state:resolved]
2010-12-16 22:25:09 +01:00
Jon Leighton
14b880fd03
Fix various issues with the :primary_key option in :through associations [ #2421 state:resolved]
2010-12-15 17:50:14 -08:00
Jon Leighton
09ddca67ac
Fix problem with duplicated records when a :uniq :through association is preloaded [ #2447 state:resolved]
2010-12-15 17:47:42 -08:00
Pavel Gorbokon
96bae30538
Replace rudimentary named_scope with scope. [ #6052 state:resolved]
...
* rename method names (actually in tests)
* rename instance variable @_named_scopes_cache to @_scopes_cache
* rename references in doc comments
* don't touch CHANGELOG :)
2010-12-15 14:02:30 -08:00