From e0002ec727cf49bef9fccd0252a5d7eb14633e50 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Thu, 4 Feb 2016 17:52:25 -0800 Subject: [PATCH] Recursively import all has_many/has_one associations, not just autosave --- lib/activerecord-import/import.rb | 13 ++++++++----- test/models/book.rb | 6 +++--- test/models/topic.rb | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/activerecord-import/import.rb b/lib/activerecord-import/import.rb index f136992e..f060b82c 100644 --- a/lib/activerecord-import/import.rb +++ b/lib/activerecord-import/import.rb @@ -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 @@ -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) } @@ -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) diff --git a/test/models/book.rb b/test/models/book.rb index 17ce4cc3..1eaf455e 100644 --- a/test/models/book.rb +++ b/test/models/book.rb @@ -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 diff --git a/test/models/topic.rb b/test/models/topic.rb index 96951596..941655f4 100644 --- a/test/models/topic.rb +++ b/test/models/topic.rb @@ -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"