Skip to content

Commit

Permalink
place the id return into every model and mark the model as clean? (si…
Browse files Browse the repository at this point in the history
…nce it has been saved)
  • Loading branch information
robmathews committed Jul 3, 2013
1 parent 53ca11b commit 6364198
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
include ActiveRecord::Import::PostgreSQLAdapter

alias_method :post_sql_statements_orig, :post_sql_statements
def post_sql_statements( table_name, options ) # :nodoc:
post_sql_statements_orig(table_name, options).append(" RETURNING ID")
end

end

4 changes: 2 additions & 2 deletions lib/activerecord-import/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def insert_many( sql, values, *args ) # :nodoc:
if NO_MAX_PACKET == max or total_bytes < max
number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql
ids = insert( sql2insert, *args )
ids = select_values( sql2insert)
else
value_sets = self.class.get_insert_value_sets( values, sql_size, max )
value_sets.each do |values|
number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql
ids.concat(select_many( sql2insert))
ids.concat(select_values( sql2insert))
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ def import( *args )
sync_keys = options[:synchronize_keys] || [self.primary_key]
synchronize( options[:synchronize], sync_keys)
end

# if we have ids, then set the id on the models and mark the models as clean.
return_obj.ids.each_with_index do |obj, index|
models[index].id = obj.to_i
models[index].instance_variable_get(:@changed_attributes).clear # mark the model as saved
end
return_obj.num_inserts = 0 if return_obj.num_inserts.nil?
return_obj
end
Expand Down

0 comments on commit 6364198

Please sign in to comment.