Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively import all has_one/has_many associations #243

Merged
merged 2 commits into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,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 @@ -516,7 +516,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 @@ -533,7 +532,10 @@ 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_one) +
model.class.reflect_on_all_associations(:has_many)
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_f >= 4.1
enum status: [:draft, :published]
end
Expand Down
2 changes: 1 addition & 1 deletion test/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Question < ActiveRecord::Base
has_one :rule, autosave: true
has_one :rule
end
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