Adrian DeGus

Joined

2,530 Experience
10 Lessons Completed
1 Question Solved

Activity

Posted in has_many :through association woes

I played around with the has_many association and it turned out that I did need to specify both has_many :project_assignments and has_many :task_assignments

But your @project = current_user.projects.create!(project_params) suggestion did the trick! Projects now show in the browser and project user_id's insert properly into both the projects and project_assignments tables. So that's awesome and I appreciate the help.

Now the only lingering problem is that user id's & task id's do not insert into the task_assignments join table. I've tried a few different ways of assigning the id's, with no luck. That last two attempts were:

@project = Project.find(params[:project_id])
@task = @project.tasks.new(task_params)
@task.update(user_id: current_user.id)
@task.save!
redirect_to @project

@project = Project.find(params[:project_id])
@task = @project.tasks.new(task_params)
@task.user_id = current_user.id
@task.save
redirect_to @project

I'm apparently missing the magic formula to bring my task join table into play.

Posted in has_many :through association woes

Crazy how you can stare at code for so long and still manage to miss something obvious like this, I hadn't noticed I was calling new twice.

user_id's are being assigned to projects properly now, but the projects still aren't displaying in the browser. Both tasks and comments display fine.

Looks like nothing is posting to either of my join tables.

project and task tables are being populated while project_assignments and task_assignments aren't.

How can I change this?

Posted in has_many :through association woes

I can build the associations fine via the console, projects just don't show as associated in the browser.

Anyone know why neither of these work from projects#create?

@project = current_user.projects.new(project_params)
@project.update(user_id: current_user.id)

Posted in has_many :through association woes

LOGS

Started POST "/projects" for ::1 at 2017-02-01 15:28:03 -0700

Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YBxwzBvHkXAqibYtTZVZI+5BIecKTyzR3hGglcghyfHLOVdTjGkBvl6EPkY+hRSIGE0HXUI7MWvIOxd7AkeZbw==", "project"=>{"project_title"=>"Poolside"}, "commit"=>"+"}

[1m[36mUser Load (0.0ms)[0m [1m[34mSELECT users.* FROM users WHERE users.id = 1 ORDER BY users.id ASC LIMIT 1[0m
[1m[35m (0.5ms)[0m [1m[35mBEGIN[0m
[1m[35mSQL (2.5ms)[0m [1m[32mINSERT INTO projects (project_title, created_at, updated_at) VALUES ('Poolside', '2017-02-01 22:28:03', '2017-02-01 22:28:03')[0m

Looks like the correct user_id 1 was selected from the db.
But in the console user_id is nil and nothing shows in the browser.

Strong params only need to cover project title, details & due date.

Not familar with accepts_nested_attributes_for

Posted in has_many :through association woes

I have a has_many :through relationship between a few models that almost works.

My goal is to allow users to be assigned to projects and/or tasks. Everything seems to work except user_id's are not being added to new projects. Tasks and comments are fine.

I tried adding user_id's to projects in the console but they do not display in the browser while logged into the user account.

Here's what I'm working with:

PROJECT_ASSIGNMENT MODEL (project join model)

  belongs_to :user
  belongs_to :project

TASK_ASSIGNMENT MODEL (task join model)

  belongs_to :user
  belongs_to :task

USER MODEL

has_many :project_assignments
has_many :projects, through: :project_assignments
has_many :task_assignments
has_many :tasks, through: :task_assignments
has_many :comments

PROJECT MODEL

  has_many :project_assignments
  has_many :users, through: :project_assignments
  has_many :tasks, dependent: :destroy

TASK MODEL

  has_many :task_assignments
  has_many :users, through: :task_assignments
  belongs_to :project
  has_many :comments, dependent: :destroy

COMMENT MODEL

  belongs_to :task
  belongs_to :user

PROJECTS#CREATE

    @project = Project.new(project_params)
    @project = current_user.projects.new(project_params)
    @project.save
    redirect_to :back

@project = current_user.projects.new(project_params) doesn't work and neither does @project.update(user_id: current_user.id)

TASKS#CREATE

    @project = Project.find(params[:project_id])
    @task = @project.tasks.new(task_params)
    @task.user_id = current_user.id
    @task.save
    redirect_to @project

COMMENTS#CREATE

    @project_id = params[:project_id]
    @task_id = params[:task_id]
    comment = Comment.new(comment_params)
    comment.update(task_id: @task_id, user_id: current_user.id)
    comment.save!
    redirect_to project_task_path(@project_id, @task_id)

What gives?

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.

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?

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?

Posted in How to make form fields to display placeholder text?

Thanks Alan, but not sure I understand. But since this is just a prototype / practice project I'll have to revisit this when I have a bit more experience :)

Posted in How to make form fields to display placeholder text?

Thanks so much for your help Jacob, this worked perfectly!

I agree this is all very hacky, hopefully I'll be able to come around and improve on it in the future. But for now I can check it off my list.

Posted in What do you listen to while coding?

I've listened to FC Kahuna for years, but not for awhile, time to pull them back up.

I have a couple of Spotify playlists that I listen to daily while working, one is called Zone which is stuffed with downtempo electronica and another is a public liquid drum 'n bass playlist... both are rhythmic and mostly non-vocal which helps with concentration.

Posted in How to make form fields to display placeholder text?

Great thanks Alan!

Jacob, I've tried jquery with no luck, but will keep this in my back pocket in case I need to give it another shot.

Posted in How to make form fields to display placeholder text?

Hey shaky, I already have placeholders defined, they show when there's no current values pre-populating them. The problem is once I'm on projects#show or tasks#show when the values override the placeholders.

Posted in How to make form fields to display placeholder text?

I'm working on a project management app that is organized into 3 panes:

Project list | Task list | Task details

views/_sidebar: has a form to create projects
projects#show: has a form to create tasks as well as forms to create project details
tasks#show: has a form to create tasks as well as forms to create task details

The problem this caused was that the forms worked fine to create new records, up until records were created.

root
https://www.screencast.com/t/wcWMfdlW

  • project form posts as expected

projects#show
https://www.screencast.com/t/VbhQO3PbTpQ

  • project form now patches rather than posts
  • task form posts

tasks#show
https://www.screencast.com/t/qLEjgQkY1u

  • both project and task forms patch rather post

Someone on SO helped me fix this by having me assign new objects using the existing object attributes, forcing the forms to post.

# Tasks Controller
def show
   @projects = Project.all
   @project = Project.find(params[:project_id])
   @task = Task.find(params[:id])
   @new_task = Task.new(title: @task.title)
   @tasks = @project.tasks.all
end

# ...

#Form
<%= form_for([@project, @new_task]) do |f| %>
    <%= f.text_field :title %>
    <%= f.submit %>
<% end %>

Now the forms post which is great, but they're pre-populated with current values which isn't great.

I need to find a way to clear the forms so they always display the "New Project" and "Add a Task" placeholders.

Any ideas on how I might accomplish this?

I'm open to changing the @new_task hack if there's a better way to control the forms.

Posted in link_to question

task.project.id and task.project.project_title did the trick Jack, thanks!

Posted in link_to question

I'm working on a project management app that has a dashboard which links to tasks that are overdue, due today, this week, next week etc.

I can display the task titles fine but can't get the links to work. I also want to display the associated project each linked task belongs to, but can't get that to work either.

The problem seems to be my inability to call the associated project ID for each task.

Controller:

def index
    @project = Project.new
    @projects = Project.all
    @tasks = @project.tasks.all 
    # Custom Scopes
    @tasks_overdue = Task.overdue
    @tasks_today = Task.today
end

View:

<% @tasks_overdue.each do |task| %>
      <%= link_to task.title, project_task_path(@project, task) %>
      <br>
      <%= @project.project_title %>
<% end %>

Since this page is on today#index I can't pull an ID from the address bar. Normally something like @tasks = @project.tasks.all works fine, just not in this case.

Is there any way I can pull the ID for the project associated with each task, since I can iterate through those without issue?