@@ -4,19 +4,109 @@ because of a scope or the options used. This can result in unnecessary
4
4
queries in some circumstances. ` :inverse_of ` must be manually specified
5
5
for associations to work in both ways, or set to ` false ` to opt-out.
6
6
7
+ ### Example:
8
+ # good
9
+ class Blog < ApplicationRecord
10
+ has_many :posts
11
+ end
12
+
13
+ class Post < ApplicationRecord
14
+ belongs_to :blog
15
+ end
16
+
7
17
### Example:
8
18
# bad
9
19
class Blog < ApplicationRecord
10
- has_many :recent_posts, -> { order(published_at: :desc) }
20
+ has_many :posts, -> { order(published_at: :desc) }
21
+ end
22
+
23
+ class Post < ApplicationRecord
24
+ belongs_to :blog
11
25
end
12
26
13
27
# good
14
28
class Blog < ApplicationRecord
15
- has_many(:recent_posts ,
29
+ has_many(:posts ,
16
30
-> { order(published_at: :desc) },
17
31
inverse_of: :blog
18
32
)
19
33
end
20
34
35
+ class Post < ApplicationRecord
36
+ belongs_to :blog
37
+ end
38
+
39
+ # good
40
+ class Blog < ApplicationRecord
41
+ with_options inverse_of: :blog do
42
+ has_many :posts, -> { order(published_at: :desc) }
43
+ end
44
+ end
45
+
46
+ class Post < ApplicationRecord
47
+ belongs_to :blog
48
+ end
49
+
50
+ ### Example:
51
+ # bad
52
+ class Picture < ApplicationRecord
53
+ belongs_to :imageable, polymorphic: true
54
+ end
55
+
56
+ class Employee < ApplicationRecord
57
+ has_many :pictures, as: :imageable
58
+ end
59
+
60
+ class Product < ApplicationRecord
61
+ has_many :pictures, as: :imageable
62
+ end
63
+
64
+ # good
65
+ class Picture < ApplicationRecord
66
+ belongs_to :imageable, polymorphic: true
67
+ end
68
+
69
+ class Employee < ApplicationRecord
70
+ has_many :pictures, as: :imageable, inverse_of: :imageable
71
+ end
72
+
73
+ class Product < ApplicationRecord
74
+ has_many :pictures, as: :imageable, inverse_of: :imageable
75
+ end
76
+
77
+ ### Example:
78
+ # bad
79
+ # However, RuboCop can not detect this pattern...
80
+ class Physician < ApplicationRecord
81
+ has_many :appointments
82
+ has_many :patients, through: :appointments
83
+ end
84
+
85
+ class Appointment < ApplicationRecord
86
+ belongs_to :physician
87
+ belongs_to :patient
88
+ end
89
+
90
+ class Patient < ApplicationRecord
91
+ has_many :appointments
92
+ has_many :physicians, through: :appointments
93
+ end
94
+
95
+ # good
96
+ class Physician < ApplicationRecord
97
+ has_many :appointments
98
+ has_many :patients, through: :appointments
99
+ end
100
+
101
+ class Appointment < ApplicationRecord
102
+ belongs_to :physician, inverse_of: :appointments
103
+ belongs_to :patient, inverse_of: :appointments
104
+ end
105
+
106
+ class Patient < ApplicationRecord
107
+ has_many :appointments
108
+ has_many :physicians, through: :appointments
109
+ end
110
+
21
111
@see http://guides.rubyonrails.org/association_basics.html#bi-directional-associations
22
112
@see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
0 commit comments