ruby on rails - CSV file appearing backwards and RSpec failing -


i new ruby in general , cannot find solution why entries_2.csv imported backwards. other .csv import appears same syntax , working fine.

here portion of rspec .csv method:

require_relative "../models/address_book"  rspec.describe addressbook  let(:book) {addressbook.new}  def check_entry(entry, expected_name, expected_phone_number, expected_email)     expect(entry.name).to eq(expected_name)     expect(entry.phone_number).to eq(expected_phone_number)     expect(entry.email).to eq(expected_email) end  describe "#import csv"      "imports correct number of entries"          book.import_from_csv("entries.csv")         book_size = book.entries.size          expect(book_size).to eq 5      end      "imports 1rst entry"          book.import_from_csv("entries.csv")         entry_one = book.entries[0]         check_entry(entry_one, "bill", "555-555-4854", "bill@blocmail.com")      end      "imports 2nd entry"          book.import_from_csv("entries.csv")         entry_two = book.entries[1]         check_entry(entry_two, "bob", "555-555-5415", "bob@blocmail.com")      end      "imports 3rd entry"          book.import_from_csv("entries.csv")         entry_three = book.entries[2]         check_entry(entry_three, "joe", "555-555-3660", "joe@blocmail.com")       end      "imports 4th entry"          book.import_from_csv("entries.csv")         entry_four = book.entries[3]         check_entry(entry_four, "sally", "555-555-4646", "sally@blocmail.com")      end      "imports 5th entry"          book.import_from_csv("entries.csv")         entry_five = book.entries[4]         check_entry(entry_five, "sussie", "555-555-2036", "sussie@blocmail.com")      end      "imports additional 3 entries"          book.import_from_csv("entries_2.csv")         book_size = book.entries.size          expect(book_size).to eq 3      end      "imports 1rst entry additional csv"          book.import_from_csv("entries_2.csv")         entry_one = book.entries[0]         check_entry(entry_one, "ralph", "222-222-2222", "ralph@comcast.net")      end      "imports 2nd entry additional csv"          book.import_from_csv("entries_2.csv")         entry_two = book.entries[1]         check_entry(entry_two, "will", "333-333-3333","will@comcast.net")      end      "imports 3rd entry additional csv"          book.import_from_csv("entries_2.csv")         entry_three = book.entries[2]         check_entry(entry_three, "foobarr", "444-444-4444", "foobarr@comcast.net")      end end 

here 2 .csv files:

entries.csv

name,phone_number,email bill,555-555-4854,bill@blocmail.com bob,555-555-5415,bob@blocmail.com joe,555-555-3660,joe@blocmail.com sally,555-555-4646,sally@blocmail.com sussie,555-555-2036,sussie@blocmail.com 

entries_2.csv

name,phone_number,email ralph,222-222-2222,ralph@comcast.net will,333-333-3333,will@comcast.net foobarr,444-444-4444,foobarr@comcast.net 

here error receiving when checking rspec:

failures:

1) addressbook#import csv imports 1rst entry additional csv failure/error: expect(entry.name).to eq(expected_name)

   expected: "ralph"         got: "foobarr"     (compared using ==)  # ./spec/address_book_spec.rb:8:in `check_entry'  # ./spec/address_book_spec.rb:142:in `block (3 levels) in <top (required)>' 

2) addressbook#import csv imports 2nd entry additional csv failure/error: expect(entry.name).to eq(expected_name)

   expected: "will"         got: "ralph"     (compared using ==)  # ./spec/address_book_spec.rb:8:in `check_entry'  # ./spec/address_book_spec.rb:150:in `block (3 levels) in <top (required)>' 

3) addressbook#import csv imports 3rd entry additional csv failure/error: expect(entry.name).to eq(expected_name)

   expected: "foobarr"         got: "will"     (compared using ==)  # ./spec/address_book_spec.rb:8:in `check_entry'  # ./spec/address_book_spec.rb:158:in `block (3 levels) in <top (required)>' 

finished in 0.05492 seconds (files took 0.2885 seconds load) 17 examples, 3 failures

why entries.csv rspec correct, entries_2.csv not? missing.

thanks

assuming addressbook activerecord model has_many :entries defined on it, core issue you're running relations not guaranteed returned in specific order, unless explicitly request it. this:

book.entries[0]

is running:

select * entries address_book_id = ?

and fetching first element of array. since above query not ordered, order arbitrary (as you're seeing; sometimes reflects creation order, , not).

what want requests records ordered in sequence created in, like:

book.entries.order('entries.id asc')[0]

which can accomplish adding order: 'id asc' parameter has_many :entries definition.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -