diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 26 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 16 | ||||
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 5 |
4 files changed, 35 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 55c0cdd808..3e7c787dee 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -59,14 +59,14 @@ module ActiveRecord def set_association_collection_records(id_to_record_map, reflection_name, associated_records, key) associated_records.each do |associated_record| - mapped_records = id_to_record_map[associated_record[key].to_i] + mapped_records = id_to_record_map[associated_record[key].to_s] add_preloaded_records_to_collection(mapped_records, reflection_name, associated_record) end end def set_association_single_records(id_to_record_map, reflection_name, associated_records, key) associated_records.each do |associated_record| - mapped_records = id_to_record_map[associated_record[key].to_i] + mapped_records = id_to_record_map[associated_record[key].to_s] mapped_records.each do |mapped_record| mapped_record.send("set_#{reflection_name}_target", associated_record) end @@ -78,7 +78,7 @@ module ActiveRecord ids = [] records.each do |record| ids << record.id - mapped_records = (id_to_record_map[record.id] ||= []) + mapped_records = (id_to_record_map[record.id.to_s] ||= []) mapped_records << record end ids.uniq! @@ -115,7 +115,7 @@ module ActiveRecord source = reflection.source_reflection.name through_records.first.class.preload_associations(through_records, source) through_records.each do |through_record| - add_preloaded_record_to_collection(id_to_record_map[through_record[through_primary_key].to_i], + add_preloaded_record_to_collection(id_to_record_map[through_record[through_primary_key].to_s], reflection.name, through_record.send(source)) end end @@ -140,7 +140,7 @@ module ActiveRecord source = reflection.source_reflection.name through_records.first.class.preload_associations(through_records, source) through_records.each do |through_record| - add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_i], + add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_s], reflection.name, through_record.send(source)) end end @@ -195,18 +195,22 @@ module ActiveRecord records.each do |record| if klass = record.send(polymorph_type) klass_id = record.send(primary_key_name) - - id_map = klasses_and_ids[klass] ||= {} - id_list_for_klass_id = (id_map[klass_id] ||= []) - id_list_for_klass_id << record + if klass_id + id_map = klasses_and_ids[klass] ||= {} + id_list_for_klass_id = (id_map[klass_id.to_s] ||= []) + id_list_for_klass_id << record + end end end klasses_and_ids = klasses_and_ids.to_a else id_map = {} records.each do |record| - mapped_records = (id_map[record.send(primary_key_name)] ||= []) - mapped_records << record + key = record.send(primary_key_name) + if key + mapped_records = (id_map[key.to_s] ||= []) + mapped_records << record + end end klasses_and_ids = [[reflection.klass.name, id_map]] end diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 63306644fb..4f4ba83a47 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -603,13 +603,25 @@ module ActiveRecord #:nodoc: # ==== Examples # # Create a single new object # User.create(:first_name => 'Jamie') + # # # Create an Array of new objects # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}]) - def create(attributes = nil) + # + # # Create a single object and pass it into a block to set other attributes. + # User.create(:first_name => 'Jamie') do |u| + # u.is_admin = false + # end + # + # # Creating an Array of new objects using a block, where the block is executed for each object: + # User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}]) do |u| + # u.is_admin = false + # end + def create(attributes = nil, &block) if attributes.is_a?(Array) - attributes.collect { |attr| create(attr) } + attributes.collect { |attr| create(attr, &block) } else object = new(attributes) + yield(object) if block_given? object.save object end diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index f5b2e73da9..7d5fd35dae 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -469,8 +469,8 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash) fixtures.size > 1 ? fixtures : fixtures.first end - def self.cache_fixtures(connection, fixtures) - cache_for_connection(connection).update(fixtures.index_by { |f| f.table_name }) + def self.cache_fixtures(connection, fixtures_map) + cache_for_connection(connection).update(fixtures_map) end def self.instantiate_fixtures(object, table_name, fixtures, load_instances = true) @@ -526,7 +526,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash) end end - cache_fixtures(connection, fixtures) + cache_fixtures(connection, fixtures_map) end end end diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 1d12ea8ad7..5ca51c014c 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -873,11 +873,12 @@ module ActiveRecord # Creates an object just like Base.create but calls save! instead of save # so an exception is raised if the record is invalid. - def create!(attributes = nil) + def create!(attributes = nil, &block) if attributes.is_a?(Array) - attributes.collect { |attr| create!(attr) } + attributes.collect { |attr| create!(attr, &block) } else object = new(attributes) + yield(object) if block_given? object.save! object end |