What is the difference between select,pluck,map and collect?

Hello friends, 


I am going to show you what exactly difference between all this keywords.


Below screen shot i have 3 different user record, which we are getting with all this queries.



1) User.all.select(:email)

select takes (0.3ms) when find user by email and returns Active record object.

2) User.all.pluck(:email)
pluck also takes (0.3ms) when find user by email and returns email Array.

3) User.all.collect(&:email)
collect takes (0.4ms) when find user by email and returns email Array.

4) User.all.map(&:email)

 map takes (0.5ms) when find user by email and returns email Array.


You can also try it on your rails console.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s