Simple Reference to ActiveRecord

This document provides a simple reference to ActiveRecord methods, which can be used in a rails controller or in the rails console. The examples are based on the Airport model, which can be created with the following commands:

   rails generate model Airport city:string code:string
   rails db:migrate

The model (class) name is Airport. Note that replacing model with scaffold in the above command will create both the model and the scaffold code for the application.

The methods are categorized into two groups: those that can be called with a model name (class methods) and those that can be called with an object (instance methods). The examples will use airport1 and airport2 as example objects (records) that belong to the Airport class.

Class methods

  • new --- create a new object that belongs to the model. Example: airport1 = Airport.new will create an empty airport object and assign it to the variable airport1. Note: this method is often called a constructor.
  • all --- produce a list of all objects (records) in the table. Example: alist = Airport.all will produce a list of all airport records and store them in the variable alist.
  • order --- produce a list of all objects (records) in the table ordered by the given attribute. Example: alist = Airport.order("code") will produce a list of all airport records sorted by the code attribute and store them in the variable alist. Add 'DESC' after the attribute to make them go in the opposite order: Airport.order("code DESC").
  • where --- produce a list of objects (records) that satisfy the given condition. The condition can be in the form of attribute-value hashes or as a condition string that appears in the SQL statement. Example: Airport.where(:code => "DTW") or Airport.where("code = 'CLE'"). Note: the order method can be called on the result of a where method.
  • find --- find a specific record given its id. Example: airport1 = Airport.find(3) will retrieve the airport record with an id of 3 and assign it as an object to the variable airport1.
  • find_by_attribute-name --- find the first record whose attribute matches the given attribute. Returns nil if there is no match. Example: airport2 = Airport.find_by_code("DTW") will retrieve the first airport record whose code attribute is "DTW" and assign it as an object to the variable airport2.
  • find_all_by_attribute-name --- finds all the records whose attribute matches the given attribute and returns the matching records in an array (list). Example: list = Airport.find_all_by_code("DTW") retrieves all matching records and assigns them as an array to the variable list.

Object methods

  • attribute-name --- references the attribute for the object. Example: airport2.code = "CLE" and my_code = airport2.code.
  • save --- stores the object as a record in the database table, provided that no validations are violated. Returns true if successful; otherwise, false is returned. Example: airport1.save.
  • destroy --- delete the object from the database table. Example: airport1.destroy.