In this post, we will see a list all the basic data operations like create, search, insert, update, delete etc. in a Rails application.

Let us assume we have a table named employees in database and the model for it is Employee.

The fields for the table are:

 id, integer, auto_increment
 first_name, varchar
 last_name, varchar
 gender, varchar
 date_of_birth, datetime

  1. Create a new employee with individual fields
    @employee = Employee.new
    
    @employee.first_name = "Ashutosh"
    @employee.last_name = "Pandey"
    @employee.gender = "Male"
    @employee.date_of_birth = Date.parse("1979-06-06")
    
    @employee.save
    

    @ sign is used to indicate instance variable which is available throughout the class. Variable without @ would be local variable.

  2. Create a new employee in one line
    Employee.create( first_name: 'Ashutosh', last_name: 'Pandey', gender: 'Male', date_of_birth: Date.parse("1979-06-06") )
  3. Create a new employee from form fields
    @employee = Employee.new params[:employee]
    @employee.save
  4. Finding single employee by id
    @employee = Employee.find(id)
  5. Finding single employee by conditions ( will return nil if not found )
    @employee = Employee.find_by first_name: "Ashutosh"
    @employee = Employee.find_by first_name: "Ashutosh", last_name: "Pandey"
  6. Finding employees by conditions
    @employee = Employee.where(:first_name => "Ashutosh").first
    @employee = Employee.where(:first_name => "Ashutosh").last
    @employees = Employee.where(:first_name => "Ashutosh", :gender => "Male")
    @employees = Employee.where(:id => 10..20)             # id in the range
    @employees = Employee.where(:id => [3,5,7])            # id in (3,5,7)
  7. Finding employees by using magic methods find_by_* ( will pick first matching record )
    @employee = Employee.find_by_first_name("Ashutosh")
    @employee = Employee.find_by_first_name_and_gender("Ashutosh", "Male")
    @employee = Employee.find_last_by_first_name("Ashutosh")
    @employees = Employee.find_all_by_first_name("Ashutosh")
  8. Finding records with SQL like statements
    @employees = Employee.where('first_name like ?', '%ram%')
    @employees = Employee.where('id > ?', 10)
    @employees = Employee.where('first_name like ? and gender = ?', '%am%', 'Male')
  9. Ordering records
    @employee = Employee.where(:gender => "Male").order(:first_name)
    @employee = Employee.where(:gender => "Male").order(:first_name).reverse_order
  10. Aggregate functions
    #here we assume that we have a table Sale with fields employee_id, product_amount etc.
    count = Sale.where(:employee_id => 5).count
    sum = Sale.where(:employee_id => 5).sum(:product_amount)
    average = Sale.where(:employee_id => 5).average(:product_amount)
    maximum = Sale.maximum(:product_amount)
    minimum = Sale.minimum(:product_amount)
  11. Limiting records
    @employees = Employee.where(:gender => "Male").limit(10)
  12. Records offset
    @employees = Employee.where(:gender => "Male").limit(10).offset(5)
  13. Pluck: Selecting specific columns
    @employees = Employee.where(:gender => "Male").pluck(:first_name, :last_name)
  14. Deleting an employee
    @employee = Employee.find(id)
    @employee.destroy