Last active
February 23, 2023 16:36
-
-
Save profh/c2c8aa262d15b33e5c155425488301bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Active Record provides a rich API for accessing data within a database. | |
# Below are a few examples of different data access methods provided by Active Record. | |
Animal.all | |
# return a collection with all animals | |
Animal.first | |
# return the first animal added | |
Animal.last | |
# return the last animal added | |
Animal.find(4) | |
# return the animal having id=4 | |
Animal.find_by(name: 'Dog') | |
# return the first animal named dog | |
Animal.find_by_name 'Dog' | |
# return the first animal named dog | |
Animal.exists?(4) | |
# check if animal with id=4 exists in the animals table. | |
Animal.exists?(name: "bird") | |
# check if animal named "bird" exists in the animals table; change to "Bird". | |
Animal.count | |
# count the number of animal objects created | |
Animal.find_by(active: 'true') | |
# return the first active animal record from the animals table | |
Animal.where(active: 'true') | |
# returns all the records where active:true | |
Animal.order('name') | |
# orders all the records alphabetically by name, in an ascending way | |
Animal.order('name desc') | |
Animal.order('name').reverse | |
# orders all the records alphabetically by name, in an descending way | |
a = Animal.new | |
# creates an empty Animal object | |
a.save! | |
# saves (do without ! first to see silent fail, then with ! to see error) | |
a.name = "Turtle" | |
a.save! | |
# saves the record | |
a.destroy | |
# removes the record | |
Animal.create({name: "Snake", active: false}) | |
# alternate way of creating | |
Animal.last.delete | |
Animal.create({name: "Snake", active: false}) | |
Animal.last.destroy | |
# contrast delete vs destroy (transaction callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment