3
Hello, I am developing an engine in Rails 5 where it will be just a blog API. It will be a simple system. Post has several Passions and Passions has several Posts. I made the relationship N <->
The problem is that by sending the post JSON with the Passions Ids that I want to associate with it I can’t save them.
Post.Rb
module Feed
class Post < ApplicationRecord
has_many :post_passions, dependent: :destroy
has_many :passions, :through => :post_passions
end
end
Passion.Rb
module Feed
class Passion < ApplicationRecord
has_many :post_passions
has_many :post, :through => :post_passions
end
end
Postpassion.Rb (Join Table)
module Feed
class PostPassion < ApplicationRecord
belongs_to :passion
belongs_to :post
end
end
My goal is through a POST request in the '/feed/posts' api to be able to create a post specifying several Passions. The JSON I am sending is the following.
{
"title": "Titulo da postagem",
"description":"Decrição do post",
"passion_ids":[1,2]
}
Sending this post while looking at the log of the Rails that receives the request the attribute 'passion_ids' is not sending in post so I can allow it.
Log when sending the request
Started POST "/feed/posts" for 127.0.0.1 at 2017-03-29 08:20:00 -0300
Processing by Feed::PostsController#create as */*
Parameters: {"title"=>"Titulo da postagem", "description"=>"Decrição do post", "passion_ids"=>[1, 2], "post"=>{"title"=>"Titulo da postagem", "description"=>"Decrição do post"}}
As the 'passion_ids' is not received within Post the Permit below does not work.
params.require(:post).permit(:title,
:passion_ids,
:description)
I have a system that works this way but it is in Rails 4.2 and the registrations are made by forms and not by REST.
So, but for me to be able to register the Passions together with the Post these data should come together with Post. They come together when post = Post.new(params_post) / post.save() I would already save them with all Passions arrows.
– Maurício Júnior