Commit Graph

5107 Commits

Author SHA1 Message Date
Aaron Patterson
8bdc191994 we have a method for this, so let's use it 2011-01-05 09:51:09 -08:00
Aaron Patterson
e468a62dc8 use arel ast construction rather than generating strings 2011-01-05 09:47:13 -08:00
Aaron Patterson
35a225535f use arel to construct AST rather than generate strings 2011-01-05 09:23:30 -08:00
Aaron Patterson
3f4143eedb fixing merge errors 2011-01-04 15:16:56 -08:00
Aaron Patterson
3f7e097464 Merge remote branch 'rsim/require_deprecation'
* rsim/require_deprecation:
  require ActiveSupport deprecatation file before using deprecate method in database_statements
2011-01-04 15:09:22 -08:00
Aaron Patterson
c08fccf980 Merge remote branch 'rsim/habtm_select_fix'
* rsim/habtm_select_fix:
  Explicitly select * from has_and_belongs_to_many association tables, simplify exists? query
2011-01-04 15:08:28 -08:00
Prem Sichanugrist
ad343d7263 Use Rails 3.1 change method in 'add_' migration generator 2011-01-04 11:16:52 -08:00
Prem Sichanugrist
9e64dfad0d Use Rails 3.1 change method in model generator 2011-01-04 11:16:52 -08:00
Raimonds Simanovskis
9c1c551f25 Explicitly select * from has_and_belongs_to_many association tables, simplify exists? query
Previous version (after commit 3103296a61) was generating wrong SQL for Oracle when calling exists? method on HABTM association.
2011-01-04 17:06:33 +02:00
Raimonds Simanovskis
1d758d9086 require ActiveSupport deprecatation file before using deprecate method in database_statements
otherwise when using external ActiveRecord adapters (e.g. Oracle) database_statements might be loaded before active_support/core_ext/module/deprecation which results in NoMethodError (commit 60cf65def8 added "deprecate" call to database_statements.rb)
2011-01-04 11:21:03 +02: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
99a8d8430f Create the association scope directly rather than going through with_scope 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
31d101879f Use the association directly in other places too 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
4e194ed1e6 Rename AssociationProxy#foreign_key_present to foreign_key_present? 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
d18a27031f Add documentation for the :foreign_type option on belongs_to 2011-01-03 16:24:30 -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)
b9ce3419e5 User id instead of quoted_id to prevent double quoting. Fixes failing test for bug #6036. 2011-01-04 01:21:26 +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
f7a15d0e03 Get rid of extra_conditions param from configure_dependency_for_has_many. I can't see a particularly plausible argument for this being used by plugins, and if they really want they can just redefine the callback or whatever. Note also that before my recent commit the extra_conditions param was completely ignored for :dependent => :destroy. 2010-12-31 20:00:44 +00:00
Jon Leighton
6db9085159 And owner_quoted_id can go too 2010-12-31 20:00:44 +00:00
Jon Leighton
b0bb911fa1 Now we can drop-kick AssociationReflection#dependent_conditions into oblivion. 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
bf22b287a4 Do not use primary key on insertion when it's nil 2010-12-29 01:22:43 -02:00
Santiago Pastorino
eb2ebe7680 Simplify inspect implementation
After 304d38c053 we don't need the
new_record? check anymore.
2010-12-28 15:45:35 -02: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
897b56bb2f I N C E P T I O N: flatten_deeper works around a bug in Ruby 1.8.2. 2010-12-27 12:47:30 -07:00
José Valim
6b099975fa No need to symbolize these. 2010-12-27 09:30:36 +01:00
Aaron Patterson
9bac649fa4 try not to make so many funcalls 2010-12-26 20:22:13 -07:00
Aaron Patterson
67da590979 make our hash of klasses and ids actually have classes for keys 2010-12-26 20:15:09 -07:00
Aaron Patterson
7e91ad3f89 stop calling deprecated apis 2010-12-26 19:56:18 -07:00
Jon Leighton
9f5c18ce07 Refactor we_can_set_the_inverse_on_this? to use a less bizarre name amongst other things 2010-12-26 16:27:12 -07:00
Jon Leighton
b0498372a1 Add a HasAssociation module for common code for has_* associations 2010-12-26 19:38:04 +00:00
Jon Leighton
0c272471fe Remove AssociationProxy#dependent? - it's badly named and only used in one place 2010-12-26 19:38:04 +00: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