Skip to content

Commit

Permalink
Recursively import all has_many/has_one associations, not just autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Michaels-Ober committed Feb 5, 2016
1 parent c0a4393 commit e0002ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def support_setting_primary_key_of_imported_objects?
# existing model instances in memory with updates from the import.
# * +timestamps+ - true|false, tells import to not add timestamps \
# (if false) even if record timestamps is disabled in ActiveRecord::Base
# * +recursive - true|false, tells import to import all autosave association
# if the adapter supports setting the primary keys of the newly imported
# objects.
# * +recursive - true|false, tells import to import all has_many/has_one
# associations if the adapter supports setting the primary keys of the
# newly imported objects.
#
# == Examples
# class BlogPost < ActiveRecord::Base ; end
Expand Down Expand Up @@ -445,7 +445,6 @@ def import_associations(models, options)
# now, for all the dirty associations, collect them into a new set of models, then recurse.
# notes:
# does not handle associations that reference themselves
# assumes that the only associations to be saved are marked with :autosave
# should probably take a hash to associations to follow.
associated_objects_by_class={}
models.each {|model| find_associated_objects_for_import(associated_objects_by_class, model) }
Expand All @@ -462,7 +461,11 @@ def import_associations(models, options)
def find_associated_objects_for_import(associated_objects_by_class, model)
associated_objects_by_class[model.class.name]||={}

model.class.reflect_on_all_autosave_associations.each do |association_reflection|
association_reflections =
model.class.reflect_on_all_associations(:has_many) +
model.class.reflect_on_all_associations(:has_one)

association_reflections.each do |association_reflection|
associated_objects_by_class[model.class.name][association_reflection.name]||=[]

association = model.association(association_reflection.name)
Expand Down
6 changes: 3 additions & 3 deletions test/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Book < ActiveRecord::Base
belongs_to :topic, :inverse_of=>:books
has_many :chapters, :autosave => true, :inverse_of => :book
has_many :end_notes, :autosave => true, :inverse_of => :book
belongs_to :topic, :inverse_of => :books
has_many :chapters, :inverse_of => :book
has_many :end_notes, :inverse_of => :book
if ENV['AR_VERSION'].to_i >= 4.1
enum status: [:draft, :published]
end
Expand Down
2 changes: 1 addition & 1 deletion test/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Topic < ActiveRecord::Base
validates_presence_of :author_name
validates :title, numericality: { only_integer: true }, on: :context_test

has_many :books, :autosave=>true, :inverse_of=>:topic
has_many :books, :inverse_of => :topic
belongs_to :parent, :class_name => "Topic"

composed_of :description, :mapping => [ %w(title title), %w(author_name author_name)], :allow_nil => true, :class_name => "TopicDescription"
Expand Down

0 comments on commit e0002ec

Please sign in to comment.