task = Task.find(params[:id])
project_name = task.project.name
# > NoMethodError: undefined method `name’ for nil:NilClass
task
has no project
assigned to it, so we get an error. We can avoid it by having checks like this:
task.project && task.project.name
# or
task.project.present? && task.project.name.present?
# or
task.try(:project).try(:name)
task&.project
# returns nil
task&.project&.name
# also returns nil, instead of NoMethodError
# or returns the actual value of `name`