Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails model querying returns 11 records but no limit is set

I am querying a set of data using a custom model Stat based on ActiveRecord, using sqlite3. It is obvious that the total amount of data is more than 100, but when I do my querying of all data, it always returns 11 records, no more of other data, and I did not set any limit to the statement, but in console it just add a limit of 11. limit Below is my code:

2.5.1 001 > Stat.all

  Stat Load (2.1ms)  SELECT  "stat".* FROM "stat" LIMIT ?  [["LIMIT", 11]]

2.5.1 002 > Stat.count

  (0.4ms)  SELECT COUNT(*) FROM "stat"
>> 105

Is there any way to remove automatically added limit when I am doing this?

like image 250
EUPHORAY Avatar asked Oct 30 '18 07:10

EUPHORAY


1 Answers

Is your Rails version 5.1 or newer? Since 5.1, Rails only loads needed records.

To query all records from database, use Stat.all.to_a instead.

Note: this returns Array instead of ActiveRecord_Relation

See this PR: https://github.com/rails/rails/pull/28592

like image 132
gaotongfei Avatar answered Sep 20 '22 20:09

gaotongfei