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
Split the generator command from the output to make it easier for beginners following the context guide (#6442)
* Split the generator command from the output to make it easier for beginners following the context guide. fixes#6441
* split commands from output in guides/data_modelling/in_context_relationships.md ref: #6442
* tidy console input sections in guides/data_modelling/in_context_relationships.md
* continue splitting commands from output in guides/data_modelling/cross_context_boundaries.md
* Update guides/data_modelling/in_context_relationships.md
Co-authored-by: Rodolfo Carvalho <[email protected]>
* Update guides/data_modelling/your_first_context.md
---------
Co-authored-by: Nelson <[email protected]>
Co-authored-by: n <[email protected]>
Co-authored-by: Rodolfo Carvalho <[email protected]>
Copy file name to clipboardExpand all lines: guides/data_modelling/in_context_relationships.md
+33-5Lines changed: 33 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,28 @@ For now, categories will contain only textual information. Our first order of bu
7
7
> Sometimes it may be tricky to determine if two resources belong to the same context or not. In those cases, prefer distinct contexts per resource and refactor later if necessary. Otherwise you can easily end up with large contexts of loosely related entities. Also keep in mind that the fact two resources are related does not necessarily mean they belong to the same context, otherwise you would quickly end up with one large context, as the majority of resources in an application are connected to each other. To sum it up: if you are unsure, you should prefer separate modules (contexts).
@@ -27,8 +43,12 @@ Remember to update your repository by running migrations:
27
43
This time around, we used `mix phx.gen.context`, which is just like `mix phx.gen.html`, except it doesn't generate the web files for us. Since we already have controllers and templates for managing products, we can integrate the new category features into our existing web form and product show page. We can see we now have a new `Category` schema alongside our product schema at `lib/hello/catalog/category.ex`, and Phoenix told us it was *injecting* new functions in our existing Catalog context for the category functionality. The injected functions will look very familiar to our product functions, with new functions like `create_category`, `list_categories`, and so on. Before we migrate up, we need to do a second bit of code generation. Our category schema is great for representing an individual category in the system, but we need to support a many-to-many relationship between products and categories. Fortunately, ecto allows us to do this simply with a join table, so let's generate that now with the `ecto.gen.migration` command:
We simply enumerate over a list of category titles and use the generated `create_category/1` function of our catalog context to persist the new records. We can run the seeds with `mix run`:
90
114
91
115
```console
92
-
$ mix run priv/repo/seeds.exs
116
+
mix run priv/repo/seeds.exs
117
+
```
93
118
119
+
The output in the terminal confirms the `seeds.exs` executed successfully:
120
+
121
+
```console
94
122
[debug] QUERY OK db=3.1ms decode=1.1ms queue=0.7ms idle=2.2ms
0 commit comments