Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge nested attributes in permit + Rails

params.require(:task).permit(:summary, comments_attributes: [:id, :content])

I want to add user_id and project_id in comments_attributes.

user_id    = current_user.id
project_id = project.id

I tried with below but not working

params.require(:task).permit(:summary, comments_attributes: [:id, :content]).merge(user_id: current_user.id, comments_attributes: [user_id: current_user.id, project_id: project.id])

Please help me how can I do this?

like image 948
Dipak Panchal Avatar asked Dec 05 '16 18:12

Dipak Panchal


1 Answers

Although an old question, the right answer IMHO is this ->

In Rails 5, instead of .to_h.deep_merge you should use reverse_merge

params.require(:task).permit(:summary, comments_attributes: [:id, :content]).reverse_merge(user_id: current_user.id, comments_attributes: [user_id: current_user.id, project_id: project.id])
like image 110
Zalom Avatar answered Sep 30 '22 06:09

Zalom