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.

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

Friday, 16 September 2016

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 Exception Error.
yes, you can also use to stop exception now rails gives us some find functionality for find data to the database.

there are a lot of ways to find a record by find_by

EXAMPLE: find_by_<columnname>(columnvalue)

find_by_name here name is a model column name, you can find any record which is not in a database search by name, if it will not present in the database then it will not give you an exception.



User.find_by_name("Tom")

here "Tom" is a column value, if the tom name user is not present in the database then it will return Nil so it works like where query.

like this, you can use for any column names that are in your models. suppose we have a User model in the database and it has many column names.

  1. find_by_name
  2. find_by_email
  3. find_by_phone
  4. find_by_id
You can find value by any column names that in your model

Tuesday, 13 September 2016

What is the difference between find and where query ?

If you will find any record by id or something else then, if the record would not be present find will return Exception error it can break your application. find a user who is not in a database like this:

User.find(:id=>6)

it's wrong to find data. Now try where instead of find because if the record would not be present where will return Nil it will not break your application. find a user who is not in a database like this:

User.where(:id=>6).first

That's why use where to get the record to the database in rails :)

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