-
Notifications
You must be signed in to change notification settings - Fork 29
/
active_record_read.rb
67 lines (41 loc) · 2.1 KB
/
active_record_read.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
load 'ar.rb'
# READING DB ROWS WITH ACTIVE RECORD
# We can call .count on the Customer class to count all the rows.
# This will generate the following SQL:
# SELECT COUNT(*) FROM customers;
number_of_customers = Customer.count
puts "There are #{number_of_customers} in my customers table."
# To load-up all rows from our customers table we call .all.
all_customers = Customer.all
# The all_customers object acts like an array.
puts "Based on customers collection there are #{all_customers.size} customers."
puts "The first customer is #{all_customers[0].inspect}"
# Who is the first customer by primary key?
# Unlike .all which returns a collection of Customer objects,
# .first returns a single Customer object.
first_customer = Customer.first
# We can access this customer's columns/properties in two ways:
puts "The name of my first customer is #{first_customer[:name]}."
puts "The first customer's city is #{first_customer.city}."
# Who is the last customer by primary key?
last_customer = Customer.last
puts last_customer.name
# We can simulate SQL WHERE clauses with the .where method.
# Exact matches can be found as follows:
winnipeg_customers = Customer.where(city: 'Winnipeg')
# The where method can also take a string argument.
customers_with_a_names = Customer.where('name LIKE "a%"')
# We can also chain other SQL clauses onto our searches.
# Here we're ordering our results by city and limiting to 5 rows.
five_customers_with_a_names = Customer.where('name LIKE "a%"').order(:city).limit(5)
five_customers_with_a_names.each { |c| puts c.name }
puts "Number of Winnipeg customers is #{winnipeg_customers.size}"
# If we know the primary key we can search for it directly with find.
customer_by_id = Customer.find(9000)
puts customer_by_id.inspect
# If you want to find the first instance of a record that matches a where clause,
# use find_by. Similar to where, but returns a single object instead of a collection.
customer = Customer.find_by('name LIKE "a%"')
puts customer.name
customer = Customer.find_by(city: 'Winnipeg')
puts customer.inspect