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

Conversation

zdennis
Copy link
Owner

@zdennis zdennis commented Feb 26, 2016

Based on @sferik's work in PR #230

@zdennis zdennis closed this Feb 26, 2016
@zdennis zdennis deleted the reflect-on-all-has_many branch February 26, 2016 18:41
@zdennis zdennis restored the reflect-on-all-has_many branch February 26, 2016 18:42
@zdennis zdennis reopened this Feb 26, 2016
zdennis added a commit that referenced this pull request Feb 26, 2016
Recursively import all has_one/has_many associations
@zdennis zdennis merged commit ba909fe into master Feb 26, 2016
@zdennis zdennis deleted the reflect-on-all-has_many branch February 26, 2016 18:42
@zdennis
Copy link
Owner Author

zdennis commented Feb 26, 2016

This has been included in the 0.12.0 release.

@sferik
Copy link
Contributor

sferik commented Feb 26, 2016

👍

@rhannequin
Copy link

Hello,

First, thank you very much for this gem, it is much appreciated every day from casual to complex data imports.

I am having troubles with the multi-level feature, when trying to import models with associations. I tried to follow the multi-level example this way:

I created a blank app with two models:

$ bundle exec rails g model Book title:string
$ bundle exec rails g model Review name:string book:references

Then I added the association to the Book model:

class Book < ActiveRecord::Base
  has_many :reviews
end

I created a basic rake task with the same code as the example, to insert books with one review in each, and checking at the end if everything went good:

task test: :environment do
  Book.delete_all
  Review.delete_all

  books = []
  10.times do |i|
    book = Book.new(title: "Book ##{i}")
    book.reviews.build(name: "Review ##{i}")
    books << book
  end
  Book.import books, recursive: true

  puts "'Book' records count: #{Book.count}"
  puts "Last 'Book' reviews count: #{Book.last.reviews.count}"
  puts "'Review' records count: #{Review.count}"
end

What I have in return shows that the books were inserted but not the associated reviews:

'Book' records count: 10
Last 'Book' reviews count: 0
'Review' records count: 0

I don't know if my way of inserting associated models in database is wrong or if the multi-level feature has an issue, I thought it was the best place to warn about this behaviour has @sferik 's PR (thank you for that btw) was supposed to fix it.

This test was run on Ubuntu 15.10 with:

  • rails v4.2.5.1
  • activerecord v4.2.5.1
  • sqlite3 v1.3.11
  • activerecord-import from Github (at master@df866a4)

Thank you.

sferik pushed a commit to instacart/activerecord-import_with_callbacks that referenced this pull request Mar 3, 2016
Remove monkey patch to activerecord-import since
zdennis/activerecord-import#243 was merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants