Question: I'm working on a scavenger hunt app and I'm stuck. So I've set up the CRUD for the hunts and for the tasks associated with hunts. But I don't know how to set it up so that the user can associate a particular task with a particular hunt. I think I've got the models setup correctly, but I'm not really sure how to setup the views that allow this association of tasks with a hunt. 

```
class Hunt < ActiveRecord::Base

  has_many :tasks
  attr_accessible :name
  validates :name,  :presence => true,
                    :length   => { :maximum => 50 } ,
                    :uniqueness => { :case_sensitive => false }

end

class Task < ActiveRecord::Base

  belongs_to :hunts
  attr_accessible :name
  validates :name,  :presence => true,
                    :length   => { :maximum => 50 } ,
                    :uniqueness => { :case_sensitive => false }

end

```

I'm guessing I need write a view that shows a hunt and then lists all the available tasks. Then I need a way to for the user to "pick" a task and add it to a hunt. It's this last part that has me stumped.

Answer: 
Some good sources of information are:

1. [Ruby on Rails Guides: ActionView Helpers](http://guides.rubyonrails.org/form_helpers.html) - gives a good starting point and overview
2. [Blog on dealing with Categories](http://agilewarrior.wordpress.com/2011/10/22/rails-drop-downs/) - discusses using categories derived from the associate table and presenting them in the view
3. [Rails Casts Nested Model](http://railscasts.com/episodes/196-nested-model-form-part-1) - this shows how to deal with a nested or has\_many relationship.

Now on the question of adding the task and adding a hunt, with a form that uses the nested attributes you can have a view that displays the task, allowing to add the hunt. The adding operation may require some Javascript (as demonstrated in RailsCasts), or otherwise have popup that executes a *hunt.tasks.build* in the controller. The build, on the association will assign the current hunt id to the task that is created.

Now if you have a list of tasks pre-assigned to the hunt you would need to have the form display the select list for the tasks belonging to the hunt. Using the nested attributes in the model definition as in:

```
class Hunt < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task < ActiveRecord::Base
  belongs_to :hunts
end

```

So now on the form submit the params will be posted with the associated task id nested allowing you to do the update\_attributes.

More details are in [Rails API accepts nested attributes](http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for) and the RailsCast reference will give a step by step example.
Your `belongs_to` declaration in the `Task` class should be for `:hunt`, not `:hunts`.

As far as the view goes, it's pretty simple. Just use `f.select` (assuming your form helper variable name is `f`) on `:hunt_id`, and update the `hunt_id` attribute inside `Task`.

This will associate the `Hunt` with the `Task`.

Be sure to check out `options_for_select`, so you can display the `Hunt` name with the `hunt_id` option value in the form. I also recommend reading through the Rails guide on associations: <http://guides.rubyonrails.org/association_basics.html>