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

before_save, before_create, before_update #442

Closed
indigoviolet opened this issue Aug 21, 2017 · 3 comments
Closed

before_save, before_create, before_update #442

indigoviolet opened this issue Aug 21, 2017 · 3 comments
Labels

Comments

@indigoviolet
Copy link
Contributor

My reading of the code is that importing with validation will run the before_validation and validate callbacks, but not the before_save and before_create (or before_update) callbacks before saving.

On the other hand, the approach in https://github.com/zdennis/activerecord-import/wiki/Callbacks will run the before_save and before_create callbacks before the validations, which isn't as expected either (http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html)

Is there a reason why this is so, or am I misreading the code?

@jkowens
Copy link
Collaborator

jkowens commented Aug 22, 2017

What you have stated is completely correct. Because activerecord-import has made use of built in validations to validate records, it got the before/after validation callbacks for free. It's a little more complex now the way validations are implemented, but we tried not to change existing behavior.

You're correct too that the recommendation for before_save and before_create callbacks isn't exactly sound when combining with the validate option. I am coming more to the mindset that I'd rather see this library do less 😝 So I would possibly recommend users do validation on their models and run callbacks before doing the import:

valid_books = []
invalid_books = []

books.each do |book|
  if book.valid?
    valid_books << book
  else
    invalid_books << book
  end
end

valid_books.each do |book|
  book.run_callbacks(:save) { false }
  book.run_callbacks(:create) { false }
end

Book.import valid_books, validate: false

I suppose we could consider a PR to add running those callbacks as an option if there is a compelling need.

@jkowens jkowens added the docs label Sep 12, 2017
@dillonwelch
Copy link
Contributor

@jkowens I see you flagged this as docs, where would be the right place to update to see this documented in the project?

@jkowens
Copy link
Collaborator

jkowens commented Sep 25, 2018

@oniofchaos I would love to see the Readme get a full revamp and all of these issues I've flagged as docs I think would be good to include.

@jkowens jkowens mentioned this issue Sep 25, 2018
10 tasks
dillonwelch added a commit to dillonwelch/activerecord-import that referenced this issue Sep 26, 2018
Closes zdennis#442

Reorganize the README to explicitly have a section dedicated to usage of
activerecord-import, similar to how the wiki is currently laid out.

Add a note that the wiki is not to be added to going forward.

Migrate the callbacks wiki article to the README, plus add the example
from zdennis#442 to add an example of how callbacks and validations can
interact in a different fashion.
jkowens pushed a commit that referenced this issue Oct 3, 2018
Start the README refactor and add a section for callbacks

Closes #442

Reorganize the README to explicitly have a section dedicated to usage of
activerecord-import, similar to how the wiki is currently laid out.

Add a note that the wiki is not to be added to going forward.

Migrate the callbacks wiki article to the README, plus add the example
from #442 to add an example of how callbacks and validations can
interact in a different fashion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants