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

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