myModel.all returns only 138 records

Hi

I have a Rail 4 with Postrgesql 9.1 setup that is working fine, apart from I just ran into an issue.

Lets say I have a model called MyModel that has 150 records.

If in IRB I try:

test = MyModel.all

Only 138 records are returned.

If I now try:

lastRecord = test.last

The record I get has an ID of 138

And yet if I try:

test.count

It returns the correct value - 50!

Any ideas on what is going on?

Hi

I have a Rail 4 with Postrgesql 9.1 setup that is working fine, apart from I just ran into an issue.

Lets say I have a model called MyModel that has 150 records.

If in IRB I try:

test = MyModel.all

Only 138 records are returned.

How do you know?

If I now try:

lastRecord = test.last

The record I get has an ID of 138

If you have not specified an order clause then the order you get the records in is undefined, so the last record is not necessarily the highest id.

And yet if I try:

test.count

It returns the correct value - 50!

I presume you mean 150

Colin

Hi,

As of Rails 4, the order is by default based on the id DESC. So Model.last produces the following sql:

SELECT * FROM models ORDER BY models.id DESC LIMIT 1

That said, I’ve no idea why you only get a part of the records back.

Were you able to validates all those numbers directly on the DB?

Regards

Geoffroy

Hi,

As of Rails 4, the order is by default based on the id DESC. So Model.last produces the following sql:

SELECT * FROM models ORDER BY models.id DESC LIMIT 1

That said, I've no idea why you only get a part of the records back.

It is likely that he is getting all records back (as shown by #count) and that 138 is the lowest id.

Colin

Colin Law wrote in post #1114588:

It is likely that he is getting all records back (as shown by #count) and that 138 is the lowest id.

Nope:)

The reason I noticed is that when I did a find all I wasn't getting new records that had been added.

However, if I get the new records by their id they definitely exist.

It's got odder this morning, there are now 155 records, and a find all returns the latest records, however records id 139 to 145 are missing! Again, I can get them by id.

But using Webmin to list the records, I get exactly the same result - so it is looking like a database issue.

Colin Law wrote in post #1114588:

It is likely that he is getting all records back (as shown by #count)

and that 138 is the lowest id.

Nope:)

The reason I noticed is that when I did a find all I wasn’t getting new records that had been added.

However, if I get the new records by their id they definitely exist.

It’s got odder this morning, there are now 155 records, and a find all returns the latest records, however records id 139 to 145 are missing! Again, I can get them by id.

But using Webmin to list the records, I get exactly the same result - so it is looking like a database issue.

I’ve been trying but am unable to duplicate your problem. I’m running on rails4 with postgresql9.1 and pg (0.15.1). Here’s my simple test:

  1. rails new CheckItOut -d postgresql
  2. cd CheckItOut
  3. rails generate scaffold Number name:string value:integer
  4. rake db:create
  5. rake db:migrate
  6. rails console

n = 0 while (n < 200) {

name = sprintf(“%04d”, n) Number.create(:name => name, :value => n) n += 1 }

  1. rails server
  2. browsing to localhost:3000/numbers shows an index with 200 entries of the expected names and values

Try these steps to see if you really have the database problem as you’ve reported it or if what you’re seeing is a result of something lurking in your app.

1) Watch the rails app log to see what SQL is being issued.

2) It really sounds like somewhere you're keeping a transaction open, which is why you see different versions of the db.