forked from cgardens/playing-with-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkindness.rb
67 lines (48 loc) · 1.49 KB
/
kindness.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
require 'active_record'
`rm kindness.db`
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'kindness.db'
)
ActiveRecord::Migration.create_table :students do |t|
t.string :name
end
ActiveRecord::Migration.create_table :acts_of_kindnesses do |t|
t.integer :student_id
t.integer :recipient_id
t.integer :magnitude
t.timestamps
end
ActiveRecord::Migrator.up 'db/migrate'
# -----
class Student < ActiveRecord::Base
has_many :kindnesses, class_name: 'ActsOfKindness'
has_many :blessings, class_name: 'ActsOfKindness'
scope :random, ->{ all.sample }
end
class ActsOfKindness < ActiveRecord::Base
belongs_to :student
belongs_to :recipient, class_name: 'Student'
scope :greatest, ->{ order('magnitude DESC').limit(1).first }
scope :earliest, ->{ order('created_at DESC').limit(1).first }
end
# ---
p Student.create(name: 'Charles')
p Student.create(name: 'Sherif')
p Student.create(name: 'Banu')
p Student.create(name: 'Gabriel')
p Student.all
# make people happy
10.times do
Student.random.kindnesses.create(recipient: Student.random, magnitude: (1..100).to_a.sample)
end
# show all acts of kindness that a specific student gave?
p Student.first.kindnesses
# show all acts of kindness that a specific student received?
p Student.first.blessings
puts "-"*20
p act = ActsOfKindness.greatest
puts "#{act.student.name} was kind to #{act.recipient.name}"
puts "-"*20
p act = ActsOfKindness.earliest
puts "#{act.student.name} was kind to #{act.recipient.name}"