linkyndy
linkyndy

Reputation: 17910

Rails .all returns a Relation with objects, but .count and .all.to_a doesn't return anything

I'm currently having a very weird issue at the moment. I have an ExportFile model. Within a byebug console inside the tests, if I call .all, I see:

>>> ExportFile.all
#<ActiveRecord::Relation [#<ExportFile id: 189, sequence_number: 1, filename: "DB_EXPORT_20151106173840.zip", status: 3, content: 2, created_at: "2015-12-09 12:54:56", updated_at: "2015-12-09 12:54:56", first_transfer_date: nil, failed_transfer_count: 0, successful_transfer_date: "2015-12-01 12:54:56">, #<ExportFile id: 190, sequence_number: 2, filename: "DB_EXPORT_20151106173840.zip", status: 3, content: 2, created_at: "2015-12-09 12:54:56", updated_at: "2015-12-09 12:54:56", first_transfer_date: nil, failed_transfer_count: 0, successful_transfer_date: "2015-12-01 12:54:56">, #<ExportFile id: 191, sequence_number: 3, filename: "DB_EXPORT_20151106173840.zip", status: 3, content: 2, created_at: "2015-12-09 12:54:56", updated_at: "2015-12-09 12:54:56", first_transfer_date: nil, failed_transfer_count: 0, successful_transfer_date: "2015-12-01 12:54:56">, #<ExportFile id: 192, sequence_number: 4, filename: "DB_EXPORT_20151106173840.zip", status: 3, content: 2, created_at: "2015-12-09 12:54:56", updated_at: "2015-12-09 12:54:56", first_transfer_date: nil, failed_transfer_count: 0, successful_transfer_date: "2015-12-01 12:54:56">, #<ExportFile id: 193, sequence_number: 5, filename: "DB_EXPORT_20151106173840.zip", status: 3, content: 2, created_at: "2015-12-09 12:54:56", updated_at: "2015-12-09 12:54:56", first_transfer_date: nil, failed_transfer_count: 0, successful_transfer_date: "2015-12-01 12:54:56">]>

But if I try .count, or .all.to_a, I get nothing:

>>> ExportFile.count
0
>>> ExportFile.all.to_a
[]

Why is this happening and what am I doing wrong? Before running the above snippets I use FactoryGirl.create_list(:export_file, 5), if it does make a difference.

Upvotes: 1

Views: 297

Answers (2)

linkyndy
linkyndy

Reputation: 17910

It seemed that upgrading byebug solved this issue. I've upgraded from 8.1.0 to 8.2.1. Very strange...

Upvotes: 1

Andrey Deineko
Andrey Deineko

Reputation: 52357

I assume the thing is that you are in console in development mode, but FactoryGirl creates records in test database, thus you do not have access to them.

I think, that if you seed the development database with ExportData records, they'd be there for you.

Upvotes: 1

Related Questions