myModel.all returns only 138 records

35 views
Skip to first unread message

zip py

unread,
Jul 5, 2013, 1:06:58 PM7/5/13
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jul 5, 2013, 5:16:14 PM7/5/13
to rubyonra...@googlegroups.com
On 5 July 2013 18:06, zip py <li...@ruby-forum.com> wrote:
> 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

Geoffroy Gomet

unread,
Jul 6, 2013, 6:15:57 AM7/6/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
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

Op vrijdag 5 juli 2013 23:16:14 UTC+2 schreef Colin Law het volgende:

Colin Law

unread,
Jul 6, 2013, 8:30:10 AM7/6/13
to rubyonra...@googlegroups.com
On 6 July 2013 11:15, Geoffroy Gomet <geoffro...@gmail.com> wrote:
> 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

zip py

unread,
Jul 8, 2013, 5:26:15 AM7/8/13
to rubyonra...@googlegroups.com
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.

Rick

unread,
Jul 8, 2013, 7:00:22 AM7/8/13
to rubyonra...@googlegroups.com

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 }
7) rails server
8) 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.
Posted via http://www.ruby-forum.com/.

Scott Ribe

unread,
Jul 8, 2013, 9:30:27 AM7/8/13
to rubyonra...@googlegroups.com

On Jul 8, 2013, at 3:26 AM, zip py wrote:

> 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.

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.

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Reply all
Reply to author
Forward
0 new messages