I have method which updating data in database from CSV.
csv_text = File.read("#{Rails.root}/db/seed/books.csv")
csv = CSV.parse(csv_text, :headers => :first_row)
csv.each { |row|
row = row.to_hash.with_indifferent_access.to_hash.symbolize_keys
row.delete_if { |key, value| value.nil? }
book = Book.find_or_initialize_by_name(row[:name])
book.update_attributes!(row)
How can i check if record will be created or updated or nothing happens?
To check if
bookwas changed there’schanged?:To check if
bookis a new record there’snew_record?: