-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathactive_record.rb
68 lines (58 loc) · 1.4 KB
/
active_record.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
68
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
self.verbose = false
create_table :posts, force: true do |t|
t.string :title
t.text :body
t.references :author
t.timestamps null: false
end
create_table :authors, force: true do |t|
t.string :name
t.timestamps null: false
end
create_table :comments, force: true do |t|
t.text :contents
t.references :author
t.references :post
t.timestamp null: false
end
create_table :employees, force: true do |t|
t.string :name
t.string :email
t.timestamp null: false
end
create_table :pictures, force: true do |t|
t.string :title
t.string :imageable_type
t.string :imageable_id
t.timestamp null: false
end
end
module ARModels
class Post < ActiveRecord::Base
has_many :comments
belongs_to :author
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :posts
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
belongs_to :author
end
class CommentSerializer < ActiveModel::Serializer
attributes :id, :contents
belongs_to :author
end
class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :posts
end
end