5 ways to associate current_user with record on create
#1 Add to params #
def create
@inbox = Inbox.new(inbox_params)
@inbox.user = current_user
end
#2 Relationship #
def create
@inbox = current_user.inboxes.new(post_params)
end
#3 Merge in params #
def create
@inbox = Inbox.new(inbox_params.merge({ user: current_user }))
end
#4 Merge in private params #
def create
@inbox = Inbox.new(inbox_params)
@inbox.user = current_user
end
def inbox_params
params.require(:inbox).permit(:name).merge(user: current_user)
end
#5 Add current_user by default in model association #
Did you like this article? Did it save you some time?