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
- User.select(:id)
- User Load(0.9ms) SELECT id FROM “users”
- #<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
- User.pluck(:id)
- User Load(0.9ms) SELECT user.id FROM “users”
- [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