How to handle tag_ids
in post params to save it in related model? I would like to use for it only post_params
method.
has_many :tags
def post_params
params.require(:post).permit(:title, :message, :tag_ids)
end
#Parameters: {"post"=>{"title"=>"asdf", "message"=>"asfd", "tag_ids"=>"543d727a4261729ecd000000,543d8a914261729ecd010000"}}
I've got:
Mongoid::Errors::InvalidValue -
Problem:
Value of type String cannot be written to a field of type Array
I found solution but I don't like it, in Post model I've added:
def tag_ids=str
str.split(',').each do |id|
t = Tag.find(id)
self.tags << t
end
end