Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Calculations in Active Records Query Interface

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 313
    Comment on it

    Active records query interface provide us with some very nice features to perform calculations or mathematical operations to retrieve selected records on the basis of those mathematical calculations. Those methods are as follows:

    • Count

    Count method allows us to count the specific records which we need from our table. This can be applied on any query to count the records retrieved from the table. It can be written as follows :

    User.where(name:"Harry").count

    This will give us the number of all users whose name is Harry.

     

    • Average

    This method will give us the average (most probably a floating point number) of all the selected records. It can be written as follows:

    Employee.average("salary")

    This will give us the average salary of all the employees stored in the database.

     

    • Maximum

    This method gives us the maximum value of a field in the table.

    Student.maximum("attendance")

    This will give us the student who has got maximum attendance.

     

    • Minimum

    This method gives us the minimum value of a field in the table.

    Student.minimum("attendance")

    This will give us the student who has got minimum attendance.

     

    • Sum

    This method gives us the sum of the records of the field in the table.

    Customer.sum("order_count")

    This will give us the total orders given by customers.

 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: