diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-05-11 23:50:08 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-11 23:50:08 +0100 |
commit | cb50a2880759f311148abda55ab60be772b8aa51 (patch) | |
tree | 3f8f851f332c0976349de9dc2b4a722946b4cc5e /activerecord/lib | |
parent | 35634feb474cc55fbc95edeffe98cec241d45f23 (diff) | |
parent | 9a137506a1267ec5938fcec4d2ff135f15037459 (diff) | |
download | rails-cb50a2880759f311148abda55ab60be772b8aa51.tar.gz rails-cb50a2880759f311148abda55ab60be772b8aa51.tar.bz2 rails-cb50a2880759f311148abda55ab60be772b8aa51.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 23 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 12 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 2 |
5 files changed, 37 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index da4ebdef51..a3d1f12b03 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -31,12 +31,12 @@ module ActiveRecord private def preload_one_association(records, association, preload_options={}) - reflection = reflections[association] - raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection - - # Not all records have the same class, so group then preload. - records.group_by(&:class).each do |klass, records| - reflection = klass.reflections[association] + class_to_reflection = {} + # Not all records have the same class, so group then preload + # group on the reflection itself so that if various subclass share the same association then we do not split them + # unncessarily + records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records| + raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection send("preload_#{reflection.macro}_association", records, reflection, preload_options) end end @@ -143,7 +143,8 @@ module ActiveRecord through_primary_key = through_reflection.primary_key_name unless through_records.empty? source = reflection.source_reflection.name - through_records.first.class.preload_associations(through_records, source) + #add conditions from reflection! + through_records.first.class.preload_associations(through_records, source, reflection.options) through_records.each do |through_record| add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_s], reflection.name, through_record.send(source)) @@ -251,12 +252,12 @@ module ActiveRecord conditions << append_conditions(options, preload_options) reflection.klass.find(:all, - :select => (options[:select] || "#{table_name}.*"), - :include => options[:include], + :select => (preload_options[:select] || options[:select] || "#{table_name}.*"), + :include => preload_options[:include] || options[:include], :conditions => [conditions, ids], :joins => options[:joins], - :group => options[:group], - :order => options[:order]) + :group => preload_options[:group] || options[:group], + :order => preload_options[:order] || options[:order]) end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 74299bd572..7999eec55d 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1910,6 +1910,8 @@ module ActiveRecord #:nodoc: # { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id='4'" # "name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'" def sanitize_sql_for_conditions(condition) + return nil if condition.blank? + case condition when Array; sanitize_sql_array(condition) when Hash; sanitize_sql_hash_for_conditions(condition) diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index af4fb6e83c..5cc9f4e197 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -8,6 +8,12 @@ module ActiveRecord end end + class DuplicateMigrationNameError < ActiveRecordError#:nodoc: + def initialize(name) + super("Multiple migrations have the name #{name}") + end + end + class UnknownMigrationVersionError < ActiveRecordError #:nodoc: def initialize(version) super("No migration with version number #{version}") @@ -440,6 +446,10 @@ module ActiveRecord if klasses.detect { |m| m.version == version } raise DuplicateMigrationVersionError.new(version) end + + if klasses.detect { |m| m.name == name.camelize } + raise DuplicateMigrationNameError.new(name.camelize) + end load(file) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 81b99f8e96..d43ebefc3b 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -71,6 +71,18 @@ module ActiveRecord # end # end # + # + # For testing complex named scopes, you can examine the scoping options using the + # <tt>proxy_options</tt> method on the proxy itself. + # + # class Shirt < ActiveRecord::Base + # named_scope :colored, lambda { |color| + # { :conditions => { :color => color } } + # } + # end + # + # expected_options = { :conditions => { :colored => 'red' } } + # assert_equal expected_options, Shirt.colored('red').proxy_options def named_scope(name, options = {}, &block) scopes[name] = lambda do |parent_scope, *args| Scope.new(parent_scope, case options diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index d25e8cd0da..b3a75121ed 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -640,7 +640,7 @@ module ActiveRecord results = finder_class.with_exclusive_scope do connection.select_all( construct_finder_sql( - :select => "#{attr_name}", + :select => "#{connection.quote_column_name(attr_name)}", :from => "#{finder_class.quoted_table_name}", :conditions => [condition_sql, *condition_params] ) |