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

after_save supports block with arity of 2 for access to raw attributes #68

Merged

Conversation

macfanatic
Copy link
Contributor

This allows us to inspect the raw contents from the CSV if the record
encountered some errors.

The value in the CSV might be present, but yet when transformed
disappears if invalid (say referencing another resource that is not
accessible).

We use like so:

class Importer
  model Task

  column :assignee, to: ->(name) { User.active.find_by(name: name) }

  after_save do |task, attributes|
    if task.errors[:assignee].present? && attributes['Assignee'].present?
      task.errors.add(:assignee, "'#{ attributes['Assignee'] }' is not part of this project."
    end
  end
end

This allows us to inspect the raw contents from the CSV if the record
encountered some errors.

The value in the CSV might be present, but yet when transformed
disappears if invalid (say referencing another resource that is not
accessible).
@pcreux
Copy link
Owner

pcreux commented Jan 13, 2018

👏 (and sorry for the delay....)

@pcreux pcreux merged commit 2747863 into pcreux:master Jan 13, 2018
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.

2 participants