Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • find_by in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 139
    Comment on it

    Hi Friends,
    Rails provides several methods for fetching single object from table, one of the method is find_by. It fetches the first record in the table matching some specific conditions. Example can be if we want to fetch the first blog with title "Active Records in Rails", then we will use:

    Blog.find_by title: 'Active Records in Rails'
    # => #<blog id:1,title:"active records in rails"></blog> 
    


    It will return nil, if no record matches the specified criteria.

    One more similar method is find_by! the difference is that it returns ActiveRecord::RecordNotFound if no record is found with specified criteria. Example:

    Blog.find_by! title: 'Not found'
    # => ActiveRecord::RecordNotFound
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: