ActiveRecordObject.all.method versus ActiveRecordObject.method in Rails

I am reading the SitePoint book “Rails: Novice to Ninja”. On page 154 we are shown the method .all in relation to an ActiveRecord class/object we created earlier. This method retrieves all records.

On the next few pages, we are shown Story.all.limit and a few others. I have noticed that the limit method works without chaining it from all. I am wondering why we are using the all method here instead of directly calling the methods?

What I thought was happening is we are first getting all the records, and then filtering through those records after. However, after running Story.limit(2) I see the SQL hasn’t changed. Are there any major differences between chaining the method onto all versus not?

I admit I still have a lot more to read, but I thought I’d ask you guys first.

This is a GREAT question. If you are in the development environment your server output will show the actual SQL query that is generated by ActiveRecord. Looking at that you will notice there is some “Rails Magic” taking place; the call to SQL includes a ‘limit 2’ meaning the actual set of records returned from the database is limited. This is more efficient than retrieving ALL and parsing the list down to only [the first] two.

Thanks for the reply!

I’m not sure I’m seeing what you’re saying. Here is some terminal output using the all method and no all method:

Running via Spring preloader in process 27232
Loading development environment (Rails 5.0.0.1)
2.3.1 :001 > Story.limit(2)
  Story Load (0.9ms)  SELECT  "stories".* FROM "stories" LIMIT ?  [["LIMIT", 2]]
 => #<ActiveRecord::Relation [#<Story id: 1, name: "SitePoint", link: "https://sitepoint.com", created_at: "2016-11-06 04:51:31", updated_at: "2016-11-06 04:51:31">, #<Story id: 2, name: "SitePoint Forums", link: "http://community.sitepoint.com", created_at: "2016-11-06 04:53:02", updated_at: "2016-11-06 04:53:02">]> 
2.3.1 :002 > Story.all.limit(2)
  Story Load (0.1ms)  SELECT  "stories".* FROM "stories" LIMIT ?  [["LIMIT", 2]]
 => #<ActiveRecord::Relation [#<Story id: 1, name: "SitePoint", link: "https://sitepoint.com", created_at: "2016-11-06 04:51:31", updated_at: "2016-11-06 04:51:31">, #<Story id: 2, name: "SitePoint Forums", link: "http://community.sitepoint.com", created_at: "2016-11-06 04:53:02", updated_at: "2016-11-06 04:53:02">]> 

AFAIK the “all” is about “truthiness”

That is, it will not include false or nil

In this case it probably doesn’t make much difference with your Story except maybe for optimizing. (i.e. the execution time)

Do you get different results for these 2 commands?

Story.count()
Story.all.count()

I don’t get different results, no, but I only have 7 entries. Maybe there is a built in limit in the non-all way?

The truthiness thing seems a bit odd to me – wouldn’t there be a better way to consume that logic?

Since your “intent” in both cases is to retrieve only two records, ActiveRecord is formulating the same SQL call. It is a built-in efficiency.

Did I misunderstand your question?

No, that makes sense. It’s built in with the limit method. What about the others, though? I’m just trying to figure out why the author of the book chose to use .all.method instead of just .method.

I guess you could add a bunch more to Story to test.

But I’m thinking if it takes 0.9ms without the all and 0.1ms with the all that the difference would become greater the larger Story became.

I think that’s a built-in caching thing. Afterwards, both methods came in at 0.2s or 0.1s.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.