When I use the method .count, it works just fine and accurately shows the number of posts. When I try .size, it throws an error. Why is this?
My static_controller.rb code (with COUNT as a method)
class StaticController < ApplicationController
@post_count = Post.count
@user_count = User.count
<p> <%= @post_count %> </p>
count is an AR method, it will use
COUNT in the SQL query, and it actually makes AR aware that you are doing a database query, and it return the count value returned by the database.
Size is a method for collections, Post and User are classes, not collections, so AR is never used, and you end up with an error, because you first need to do an AR query to have a collection, even an empty one.
The rails way to do what you try is to use
all, that will return a collection.
@post_count = Post.all.size @user_count = User.all.size