Wednesday, 21 September 2016

Difference Between pluck and select

Let’s take a look with pluck and select. They both are useful methods of active record and improve the performance of your app.
Suppose we have User model
  1. User.select(:id)
  2. User Load(0.9ms) SELECT id FROM “users”
  3. #<User id: 12>,#<User id: 42>,#<User id: 1>….

In this example, select create an Active Record::Relation objects of User models where the only id is returned.
Pluck execute query like this
  1. User.pluck(:id)
  2. User Load(0.9ms) SELECT  user.id FROM “users”
  3. [1,2,3,….]

The result is the same but pluck gives an Array of integers, instead of User models. Both queries take the same time so that’s not much different between pluck and select.

Pluck  return model values, select return model objects

No comments:

Post a Comment

Featured post

Difference between find, find_by and where

Hello, friends in the last post we have read about the difference between find and where, Today I will share how to use find to stop Exc...