Friday, 23 September 2016

Difference between pluck,select,collect and map

Hello, friends in my last post I described how to fetch with pluck and select,
Today I am going to show you what exact difference between them.

In below screenshot, I have 3 different user record, which we are fetching with different queries.





 => User.all.select(:email)
select takes (0.3ms) when find user by email and returns Active record object.
=> User.all.pluck(:email)
pluck also takes (0.3ms) when find user by email and returns email Array.

=> User.all.collect(&:email)
collect takes (0.4ms) when find user by email and returns email Array.
=> User.all.map(&:email)
 map takes (0.5ms) when find user by email and returns email Array.

You can also test it on your rails console.

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...