All threads / Problem associating nested resource with Devise current_user method

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Problem associating nested resource with Devise current_user method

Adrian DeGus asked in Rails

I need to assign the current_user ID to all new projects, tasks and comments created in my app. I got projects and tasks working fine, but for some reason comments are giving me trouble.

Here's what I'm working with:

# routes

resources :projects do
    member do
      patch :complete
      patch :cancel
    end
    resources :tasks do
      member do
        patch :complete
        patch :cancel
      end
      resources :comments
    end
end
# User model

has_many :projects
has_many :tasks
has_many :comments
# Project model

belongs_to :user
# Task model

belongs_to :user
# Comments model

belongs_to :user
# projects_controller

@project = current_user.projects.new(project_params)
@project.save
# tasks_controller

@project = Project.find(params[:project_id])
@task = @project.tasks.new(task_params)
@task.user = current_user
@task.save
# comments_controller

@project = Project.find(params[:project_id])
@task = Task.find(params[:task_id])
@comment = @task.comments.new(comment_params)
@comment.user = current_user
@comment.save

Problems:

  • When belongs_to :user is in the comments model it prevents comments from saving to the db
  • When I use has_one :user, through: :task comments post but no user ID is added
  • @comment.user = current_user in comments_controller throws a no method error for user
  • @comment.user_id = current_user throws the same no method error
  • Adding @task.user into the create method in the comments_controller does nothing
  • @task = current_user.tasks.comments.create also does nothing

Why isn't the comments model/controller behaving the same as projects & tasks?

How do I get user ID's posting properly to comments?

I think you have a few calls that really aren't necessary here:

# comments_controller
@project_id = params[:project_id]
@task_id = params[:task_id]

comment = Comment.new(comment_params)
comment.update(project_id: @project_id, task_id: @task_id, user_id: current_user.id)
comment.save!

When creating an object, all that's needed is the ID's for the associations, so there's no benefit in hitting the DB to grab the project and task objects since all you're really needing them for is their ID's. You're already passing that through your params, so just use those.

Hey Jacob,

Pulling only the ID's out of the associated objects makes perfect sense, didn't realize I could do that.

So I implemented this code but am getting the error "unknown attribute 'project_id' for Comment" from comment.update(project_id: @project_id, task_id: @task_id, user_id: current_user.id)

I tried a few tweaks but couldn't get it working.

Any ideas?

Ah, you can remove project_id: @project_id if you aren't associating the comment to the project directly. Looking at your models, it doesn't look like you do, so it should be safe to remove. Sorry about that, it carried over in my mind due to a project I'm currently working on that has similar associations!

Oh wow can't believe I didn't notice that myself lol

It works like a charm now, thanks!!

I'm also excited at the other takeaways from this:

  1. I can pull only the ID's from objects when they're just needed for associations
  2. I don't have to use instance variables for everything within my methods

Very cool.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.