rails tutorials: Active Record CURD

ActiveRecord is the name of Rails’ default ORM

ORM (Object-Relational Mapping): Bridges the gap between relational databases, which are designed around mathematical Set Theory and Object-Oriented programming languages that deal with objects and their behavior. Greatly simplifies writing code for accessing the database.

Three Prerequisites

  1. ActiveRecord has to know how to find your database (when Rails
    is loaded, this info is read from config/database.yml file)
  2. (Convention) There is a table with a plural name that corresponds
    to ActiveRecord::Base subclass with a singular name
  3. (Convention) Expects the table to have a primary key named id

CURD

Create

  1. Use an empty constructor and (ghost) attributes to set the values and then call save.

    1
    2
    3
    4
    p1 = Person.new
    p1.first_name = "zhang"
    p1.email = "zrc720@gmail.com"
    p1.save
  2. Pass a hash of attributes into the constructor and then call save.

    1
    2
    p2 = Person.new(name: "zhang", email: "zrc720@gmail.com")
    p2.save
  3. Use create method with a hash to create an object and save it to the database in one step.

    1
    p3 = Person.create(name: "zhang", email: "zrc720@gmail.com")

Retrieve

  1. find(id) or find(id1, id2)
    Throws a RecordNotFound exception if not found

    1
    Person.find(1)
  2. first, last, take, all
    Return the results you expect or nil if nothing is found

    1
    2
    3
    4
    Person.first
    Person.last
    Person.take
    Person.all
  3. order(:column) or order(column: :desc)
    Allows ordering of the results. Ascending or descending

    1
    2
    Person.all.order(first_name: :desc)
    Person.all.order(:first_name)
  4. pluck
    Use pluck as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want.

    1
    2
    3
    Person.pluck(:id, :name)
    # SELECT people.id, people.name FROM people
    # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
  5. take
    Gives a record (or N records if a parameter is supplied) without any implied order. The order will depend on the database implementation. If an order is supplied it will be respected.

    1
    2
    Person.take # returns an object fetched by SELECT * FROM people LIMIT 1
    Person.take(5) # returns 5 objects fetched by SELECT * FROM people LIMIT 5
  6. where(hash)
    Enables you to supply conditions for your search

    1
    2
    3
    4
    5
    6
    7
    Person.where(name: "zhangruochi")
    Person.where(["name = ? and email = ?", "Joe", "joe@example.com"])
    ```
    7. `find_by`
    Same as where, but returns a single result or nil if a record with the specified conditions is not found
    ```ruby
    User.find_by(name: "zhangruochi")
  7. limit
    Enables you to limit how many records come back

    1
    Person.offset(1).limit(1)
  8. offset(n)
    Don’t start from the beginning; skip a few

    1
    Person.offset(1).limit(1)

Update

  1. Retrieve a record, modify the values and then call save

    1
    2
    3
    zhang = User.where(name: "zhangruochi")
    zhang.email = "lvduzhen@gmail.com"
    zhang.save
  2. Retrieve a record and then call update method passing in a hash of attributes with new values

    1
    2
    zhang = User.where(name: "zhangruochi")
    zhang.update(email: "lvduzhen@gmail.com")
  3. There is also update_all for batch updates

    1
    User.where(email: 'zrc720@gmail.com').update_all(name: 'ruochi')

Delete

  1. destroy(id) or destroy
    1
    2
    3
    4
    zhang = User.first
    zhang.destroy

    User.destroy(1)