Models: Posts and Users
Post belongs_to :user
User has_many :posts
Simple.
Assuming a few users exist, we visit the edit page for a Post.
I have a similar problem, and didn't like either of these answers much. In the rails documentation (http://guides.rubyonrails.org/action_controller_overview.html#more-examples) I see the following solution:
params.fetch(:blog, {}).permit(:title, :author)
Effectively you are supplying a default of {}, which seems to work well enough (at least for my situation).
Applying to your code, you'd have:
params.fetch(:post, {}).permit(:user_id)
I think this is reasonably clean, and seems to work in my code.
This was my immediate solution... though it seems a bit silly because why are you having to check for the post params if you are clearly in the post controller and you require them anyways. Seems very counterintuitive. Is this really the best way?
params.require(:post).permit(:user_id) if params[:post]