Skip to content


Rails - multi selects for associated models

Handling Multiple Selects (or multiple associated attributes) can be quite cumbersome. Ryan Bates has recently wrapped it very nicely in his Complex Forms Screencast. As per that simple way, here’s a how I handle it:

Model:


class Project < ActiveRecord::Base
....
has_many :assigned_tasks
has_many :users, :through => :assigned_tasks
....
def assigned_task_attributes=(assigned_attributes)
assigned_attributes.each do |attributes|
assigned_tasks.build(attributes)
end
end
end

Controller:
def create
@project_task = ProjectTask.new(params[:project_task])
@project_task.save

New/Edit Views:
<%= select_tag("project[assigned_task_attributes][][user_id]“, options_for_select(@users.collect{|u| [u.full_name, u.id]}, @project.assigned_tasks.collect{|a| a.user_id} ), {:multiple =>true, :size => 5 }) %>

Adding the virtual attribute in the model saves a ton of (ugly) work in the controller.

UPDATE: Minor tweaks are required for the update operation (remove association collection before updating the model - wrap this in a transaction)

Posted in Ruby.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.