10 Useful Active Record Methods

ActiveRecord.pluck(:methods, :description)

Active Record is a Ruby gem, which connects our database to Ruby. This allows our data to persist through multiple sessions. Additionally Active Record uses Object-Relational-Mapping to create relationships between our data, allowing us to call on those relationships through Ruby. Lastly using our database to process queries, allows for a faster, and more efficient processing time.

  1. column_names: Returns a list of column names.

2. update_all(): Updates all selected records, returns the number of records update.

3. pick(): Returns the first result value(s) from the relation.

4. pluck(): Returns an array of attribute values matching the plucked column name(s).

5. map() Returns an array of attribute values matching the plucked column name(s).

6. select(): Returns a relation object(s) corresponding to the requested value(s).

7. exists()Returns a boolean indicating if any record with the given conditions exist.

8. eql? Compares two objects and returns boolean, indicating if the items match.

Mixing Methods:

9. where().or()

  • .where: return the object(s) that matches the conditions.
  • LENGTH() in quotes is the SQL command, finds the length of the argument.
  • or Allows for a second condition.

10. join

  • .join: (Inner Join) uses the foreign key to create a “virtual table” between the two classes. Pulls in the details of the class it is being called on (ex. Country, not Traveler class). The “virtual table” will also EXCLUDE any details from relationships that don’t exist. (ex. If a Country does not have a traveler, or a traveler does not have a country, they will not be on the table.)

Citation: https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by