Ruby on Rails


ActiveRecord Query Interface All Versions

1.0
1.1
1.2
2.0
2.3
3.0
3.1
3.2
4.0
4.1
4.2
5.0

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 4

    The where method is available on any ActiveRecord model and allows querying the database for a set of records matching the given criteria.

    The where method accepts a hash where the keys correspond to the column names on the table that the model represents.

    As a simple example, we will use the following model:

    class Person < ActiveRecord::Base
      #attribute :first_name, :string
      #attribute :last_name, :string
    end
    

    To find all people with the first name of Sven:

    people = Person.where(first_name: 'Sven')
    people.to_sql # "SELECT * FROM people WHERE first_name='Sven'"
    

    To find all people with the first name of Sven and last name of Schrodinger:

    people = Person.where(first_name: 'Sven', last_name: 'Schrodinger')
    people.to_sql # "SELECT * FROM people WHERE first_name='Sven' AND last_name='Schrodinger'"
    

    In the above example, the sql output shows that records will only be returned if both the first_name and the last_name match.

    query with OR condition

    To find records with first_name == 'Bruce' OR last_name == 'Wayne'

    User.where('first_name = ? or last_name = ?', 'Bruce', 'Wayne')
    # SELECT "users".* FROM "users" WHERE (first_name = 'Bruce' or last_name = 'Wayne')
    
  • 4

    The where method on on any ActiveRecord model can be used to generate SQL of the form WHERE column_name IN (a, b, c, ...). This is achieved by passing an array as argument.

    As a simple example, we will use the following model:

    class Person < ActiveRecord::Base
      #attribute :first_name, :string
      #attribute :last_name, :string
    end
    
    people = Person.where(first_name: ['Mark', 'Mary'])
    people.to_sql # "SELECT * FROM people WHERE first_name IN ('Mark', 'Mary')"
    
  • 3

    Rails have very easy way to get first and last record from database.

    To get the first record from users table we need to type following command:

    User.first
    

    It will generate following sql query:

    SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` ASC LIMIT 1
    

    And will return following record:

    #<User:0x007f8a6db09920 id: 1, first_name: foo, created_at: Thu, 16 Jun 2016 21:43:03 UTC +00:00, updated_at: Thu, 16 Jun 2016 21:43:03 UTC +00:00 >
    

    To get the last record from users table we need to type following command:

    User.last
    

    It will generate following sql query:

    SELECT  `users`.* FROM `users`  ORDER BY `users`.`id` DESC LIMIT 1
    

    And will return following record:

    #<User:0x007f8a6db09920 id: 10, first_name: bar, created_at: Thu, 16 Jun 2016 21:43:03 UTC +00:00, updated_at: Thu, 16 Jun 2016 21:43:03 UTC +00:00 >
    

    Passing an integer to first and last method creates a LIMIT query and returns array of objects.

    User.first(5)
    

    It will generate following sql query.

    SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 5
    

    And

    User.last(5)
    

    It will generate following sql query.

    SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 5
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about ActiveRecord Query Interface? Ask Question

Topic Outline