You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter05-user-products.adoc
+5-16Lines changed: 5 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,17 +137,7 @@ end
137
137
138
138
You just have to modify the `User` model and specify the `has_many` relationship with the `depend: :destroy` option. We will see later what this method does in more detail.
139
139
140
-
.test/models/user_test.rb
141
-
[source,ruby]
142
-
----
143
-
# ...
144
-
class User < ApplicationRecord
145
-
# ...
146
-
has_many :products, dependent: :destroy
147
-
end
148
-
----
149
-
150
-
.test/models/user_test.rb
140
+
.app/models/user.rb
151
141
[source,ruby]
152
142
----
153
143
# ...
@@ -156,7 +146,7 @@ class User < ApplicationRecord
156
146
has_many :products, dependent: :destroy
157
147
end
158
148
----
159
-
149
+
<<<
160
150
And that's it. Now make a _commit_:
161
151
162
152
[source,bash]
@@ -177,7 +167,7 @@ Also an important thing about validation is to validate that every product has a
177
167
----
178
168
# ...
179
169
class ProductTest < ActiveSupport::TestCase
180
-
test "Should have a positive price" do
170
+
test "should have a positive price" do
181
171
product = products(:one)
182
172
product.price = -1
183
173
assert_not product.valid?
@@ -605,7 +595,7 @@ class Api::V1::ProductsControllerTest < ActionDispatch::IntegrationTest
@@ -759,7 +749,6 @@ $ git commit -am "Create a seed to populate database"
759
749
760
750
And as we get to the end of our chapter, it's time to apply all our modifications to the master branch by making a _merge_:
761
751
762
-
Then we create a bunch of product objects with the `FactoryBot` gem:
763
752
[source,bash]
764
753
----
765
754
$ git checkout master
@@ -770,5 +759,5 @@ $ git merge chapter05
770
759
771
760
I hope you have enjoyed this chapter. It's a long one but the code we put together is an excellent base for the core app.
772
761
773
-
In the next chapter, we will focus on customizing the output of user and product models using the gem https://github.com/Netflix/fast_jsonapi_jsonapi[fast_jsonapi]. It will allow us to easily filter the attributes to display and manage associations such as embedded objects for example.
762
+
In the next chapter, we will focus on customizing the output of user and product models using the gem https://github.com/Netflix/fast_jsonapi[fast_jsonapi]. It will allow us to easily filter the attributes to display and manage associations such as embedded objects for example.
0 commit comments