![]() When we can't cope with cast_assoc/3, it is time to use put_assoc/4. ![]() Again, because the user is simply passing a string, we don't have the ID information at hand. However, here we expect tags to be sent in a string separated by comma.įurthermore, cast_assoc/3 relies on the primary key field for each tag sent in order to decide if it should be inserted, updated or deleted. We can see an example of this in Polymorphic associations with many to many. To do so correctly, Ecto requires tags to be sent as a list of maps. The cast_assoc/3 changeset function was designed to receive external parameters and compare them with the associated data in our structs. While the constraints above sound reasonable, that's exactly what put us in trouble with cast_assoc/3. Once this data is received in the server, we will break it apart into multiple tags and associate them to the post, creating any tag that does not yet exist in the database. Now let's also imagine we want the user to input such tags as a list of words split by comma, such as: "elixir, erlang, ecto". It is important to add an index at the database level instead of using a validation since there is always a chance two tags with the same name would be validated and inserted simultaneously, passing the validation and leading to duplicated entries. Note we added a unique index to the tag name because we don't want to have duplicated tags in our database. Our migrations would look like: create table ( :posts ) do add :title, :string add :body, :text timestamps ( ) end create table ( :tags ) do add :name, :string timestamps ( ) end create unique_index ( :tags, ) create table ( :posts_tags, primary_key : false ) do add :post_id, references ( :posts ) add :tag_id, references ( :tags ) end This is a classic scenario where we would use many_to_many associations. Not only that, a given tag may also belong to many posts. Imagine we are building an application that has blog posts and such posts may have many tags. To showcase those features, we will work on a practical scenario: which is by studying a many to many relationship between posts and tags. In this guide we will learn how to use constraints and upserts. (stdlib 3.17) proc_lib.erl:226: :proc_lib.Settings View Source Constraints and Upserts (cowboy 2.9.0) /Users/shansiddiqui/Desktop/blog_app/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3 (cowboy 2.9.0) /Users/shansiddiqui/Desktop/blog_app/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3 (cowboy 2.9.0) /Users/shansiddiqui/Desktop/blog_app/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2 (phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: /4 (blog_app 0.1.0) lib/plug/debugger.ex:136: BlogAppWeb.Endpoint."call (overridable 3)"/2 ![]() (blog_app 0.1.0) lib/blog_app_web/endpoint.ex:1: _builder_call/2 (blog_app 0.1.0) lib/blog_app_web/controllers/post_controller.ex:1: _controller_pipeline/2 (blog_app 0.1.0) lib/blog_app_web/controllers/post_controller.ex:1: /2 ![]() (blog_app 0.1.0) lib/blog_app_web/templates/post/:19: anonymous fn/2 in BlogAppWeb.PostView."show.html"/1 ** (KeyError) key :changeset not found in: % When I click show Posts, this error I am getting: #PID running BlogAppWeb.Endpoint (connection #PID, stream id 6) terminated The app allows you to create a blog post and add comments if you so choose. I have followed carefully correcting errors as I go. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |