0

I get a couple of user objects in @users. and i have a task object in params[:task] Now i want to save the taskobject and add relationships between all @users and that task....

@users = User.find(session[:user_id], params[:user_task])
@task = @users.tasks.create(params[:task])
@task.owner_id = session[:user_id]      

if @task.save
  redirect_to task_path(@task)

all i get is an error like: undefined method `tasks' for Array. How do i do it?

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
Frida k
  • 41
  • 7
  • It's not really clear what you're trying to do here. How are you getting the users? What is in `params[:user_task]`? – Chris Salzberg Feb 08 '13 at 13:15
  • im getting them from user_ids. first from the logged in user from the session and also from params[:user_task], which should be some user ids from checkboxes. – Frida k Feb 08 '13 at 13:16
  • 1. I want to create the new task params[:task] 2. And add relationships to that task with all users in @users – Frida k Feb 08 '13 at 13:20
  • Can you post your model associations code? i.e. `has_many`, `belongs_to` etc.? – Chris Salzberg Feb 08 '13 at 13:26

1 Answers1

1

You need to first initialize the new task, assign its owner and save it, and if that is successful then you can create associations with all users.

Like this:

@task = Task.new(params[:task])
@task.owner_id = session[:user_id]
if @task.save
  @users = User.find(session[:user_id], params[:user_task])
  @users.each { |user| user.tasks << @task }
Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82