Reset and reseed database:
rails db:migrate:reset && rails db:seed
Launch Rails console:
rails console
Retrieve all Person
objects from database:
all_people = Person.all
SELECT "people".* FROM "people"
retrieves all rows and columns from people
table.all_people
now references Relation
(like array) of Person
objects.Iterate through array of Person
objects, printing each’s first_name
:
all_people.each do |current_person|
puts current_person.first_name
end
Retrieve all Person
objects, sorted first by last_name
, then by first_name
:
all_people_sorted = Person.order(:last_name, :first_name)
SELECT
with arguments ORDER BY "people"."last_name" ASC, "people"."first_name" ASC
runs.all_people
now references sorted Relation
of Person
objects.Iterate through sorted array of Person
objects, printing each’s last_name
and first_name
:
all_people_sorted.each do |current_person|
puts current_person.last_name + ", " + current_person.first_name
end
Exit Rails console:
exit