Comments (3)
The row will fail to import if the Ticket model validates the presence of scheme_id
. You can then select invalid rows and report them back to the user.
Ex:
class Ticket < ApplicationRecord
validates :scheme_id, presence: true
end
column :scheme_id, required: true, as: ['Scheme ID'], to: -> (scheme_id, ticket) do
ticket.scheme_id = Scheme.where(external_reference: scheme_id).pluck(:id).first
end
from csv-importer.
Thanks — unfortunately, this doesn't work for all columns.
When creating a Ticket normally, it's valid to have scheme_id
be nil
. However, when importing from the CSV, we allow an empty field to set it to nil
, but a mismatching reference should cause the row to be marked invalid.
This doesn't seem to add the error to the ticket, though, and Rails still thinks it is valid.
column :scheme_id, required: true, as: ['Scheme ID'], to: lambda { |scheme_id, ticket|
unless scheme_id.blank?
ticket.scheme_id = Scheme.where(external_reference: scheme_id).pluck(:id).first
if ticket.scheme_id.nil?
ticket.errors.add(:scheme_id, 'does not match any existing scheme')
end
end
}
The only way I can think of handling these validations would be to add a imported_from_csv
to Ticket, and set those extra validations to only run when imported_from_csv
is true. But this seems a very messy way of doing it. Is there not a way of setting an error and marking the row invalid from within the importer?
from csv-importer.
One option would be to add an instance variable to Ticket
then.
column :scheme_external_identifier, required: true, as: 'Sheme ID'
class Ticket
attr_accessor :scheme_external_reference
before_validation :set_scheme_id_from_external_reference
validates :validate_scheme_external_reference
private
def set_scheme_id_from_external_reference
return if external_reference.blank?
self.scheme_id = Scheme.where(external_reference: scheme_external_reference).pluck(:id).first
end
def validate_scheme_external_reference
errors.add(:scheme_external_reference, :invalid) if scheme_external_reference.present? && scheme_id.nil?
end
end
Some might not like adding this logic to the ActiveRecord model though. :)
from csv-importer.
Related Issues (20)
- How to find_by another model's attribute? HOT 1
- Allow manual mapping HOT 2
- Cannot access instance variable in after_build HOT 1
- skip! does not indicate which row was skipped in the report HOT 1
- Many to many association HOT 1
- Possible to bypass existing callbacks HOT 2
- Encodings HOT 1
- Encoding - German Umlaute ignored. HOT 1
- Upload file HOT 5
- Add i18n to messages HOT 2
- Exception when model.save fails HOT 2
- Memory bloat when processing large files HOT 2
- Make transactions optional HOT 4
- A normalization feature? HOT 4
- German umlauts issue HOT 3
- Ruby 2.7.1 - ArgumentError: unknown encoding name - UTF-8:UTF-8 HOT 2
- Question: Pass a parameter to an import? HOT 6
- Question: Is there a way to programmatically get the column mapping to see which columns of the CSV file are being mapped to which columns/attributes of the model?
- Rails 6.1 errors hash deprecation warning HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv-importer.