top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

myModel.all returns only 138 records on rails

0 votes
252 views

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 Jul 5, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
> 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

answer Jul 5, 2013 by anonymous
0 votes

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?

answer Jul 6, 2013 by anonymous
Similar Questions
+1 vote

I have just started with rails. I am making a attendance web app. I want to insert student attendance into attendances table and unable to do it as the form just inserts only last entry of the form. Student table and Attendances table have associations (has_many,belongs_to).

Please let me know how the form should be and controller API should look like.

+2 votes

I have found a html template where I some pages need specific css files and of course there are some common css files.

How can I best deal with it ?

+1 vote

Something like x.instance_variables.each.... { |i} .... }

Thought to be a one liner. I can't figure out how to apply strip() to each variable, since instance_variable_get returns the value. I really want something I can apply .strip! to....

+2 votes

I'd like to remove all existing constraints from an ActiveRecord::Relation and leave the rest in-tact.
I am NOT looking for #unscoped since I would like to keep any joins/order clauses around.

Additionally, I would like to re-use the constraints that were removed in another query, so the ability to call #to_sql on them would be a very nice bonus.

This is the best I've been able to hack together:

Order.where(id: 1).where(id: 2).arel.constraints[0].to_sql
 => "`orders`.`id` = 1 AND `orders`.`id` = 2"

...but that just seems wrong. As far as I can tell #constraints always has one item in it, but I don't know that will always be true.

...