Question

In my User model

def activities
    Activity.where actor_type: self.class.name
end

When I call current_user.activities.page(params[:page]) in controller , I want to know it will load all activity records or not ?

Was it helpful?

Solution

ActiveRecord is lazy-loading - it will only call the SQL query when you actually access the data in the result set.

It won't load Activity data until you do something like .all or .each or something else that actually requires access to the data.

OTHER TIPS

No, kaminari will not load all records at once. You can test it in the rails console:

current_user.activities.page(params[:page]).to_sql # => "SELECT  \"activities\".* FROM \"activities\"  WHERE \"activities\".\"user_id\" = 1 LIMIT 25 OFFSET 25"

As you can see, LIMIT and OFFSET are both involved into the SQL query. And of course, it's eager loading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top