GithubHelp home page GithubHelp logo

Comments (3)

pcreux avatar pcreux commented on September 2, 2024 1

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.

unikitty37 avatar unikitty37 commented on September 2, 2024 1

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.

pcreux avatar pcreux commented on September 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.