Updating select list

I am trying to edit or update a select list and expect the choices previously made (stored in model) to be highlighted when the edit form is brought up.
Not working though and wonder what I need to do to correct this -

The select list form element looks like this:

@categories = Category.find(:all, :order => “name”)

<%= options_from_collection_for_select @categories, :id, :name %>

In the controller I’ve added this line
@canindustry = Canindustry.find(:all,:conditions => [“candidate_id = ?”, @candidate_id])

I was thinking perhaps I could add to the options_from_collections_for_select a :selected => ‘@canindustry’. However that didn’t work.
Any suggestions ?

TIA
Stuart

http://en.wikipedia.org/wiki/Dark_ambient

Maybe I didn’t do a good job of explaining below.
I have a multiple choice select list in my edit form. When the form comes up I need it to highlight all the choices previously selected.
Just not sure how to go about it.

TIA
Stuart

Stuart Fellowes wrote:

The select list form element looks like this:

@categories = Category.find(:all, :order => “name”)

<%= options_from_collection_for_select @categories, :id, :name
%>

In the controller I’ve added this line
@canindustry = Canindustry.find(:all,:conditions => [“candidate_id = ?”,

@candidate_id])

I was thinking perhaps I could add to the
options_from_collections_for_select a :selected => ‘@canindustry’.
However
that didn’t work.
Any suggestions ?

Okay, according to the documentation:
“Selected may also be an array of values to be selected when using a
multiple select.”

Well I didn’t read that in the documentation (probably glanced over) but I assumed that was a possibility.

So it looks like you need to pass it an array of values (i.e. strings).
I haven’t tried this, but I’m guessing it’s failing because you’re

creating an array of entire Canindustry objects and passing that. You
need to get just the names.

Actually as it turns out I needed the id - so this returned the list -
@canindustry_options = Canindustry.find(:all,:conditions => [“candidate_id = ?”, @candidate_id]).map {|c| c.category_id}
Just changed name to category_id in controller code.

See if this works:

Controller:
@canindustry_options = Canindustry.find(:all,:conditions =>
[“candidate_id = ?”, @candidate_id]).map {|c| c.name}

View:
<%= options_from_collection_for_select @categories, ‘id’, ‘name’,

@canindustry_options %>

… actually, wait, what the hell?

Why are you building your options list from a query on Category, but
building your selected options from a query on Canindustry? Your

selected options should (obviously) be a subset of all options. How are
you storing your selected options? I don’t know your models, of course,
but I think your currently selected categories aren’t actually correctly

represented by a list of Canindustry objects …

Hmmm…not entirely sure what you mean here. However maybe I can explain.
Category is model that contains a list of categories. It’s not a table that is altered in any way by the user. It’s use within the application is so users may select multiple categories from it. I store user input in tables as category_id. The table is rails compliant - meaning their are 2 fields, id | name. When the user needs to see what they selected I can show them with - <%= mychoice.category.name %>

Canindustry - is a model / table is where the user selections are stored. So the table is something like | id | category_id |.

So I think , perhaps the confusion of what my intentions are is that I wish to let the user choose more, less or the same on update / edit.

For example -
User Joe
Canindustry:
(first time creating their selections)

id | candidate_id | category_id |
1 5 2
2 5 6

 3          5                     8

(second time: updating their selections)
1 5 2
2 5 5

So on the update, Joe deleted record with id 3

and changed record with id 2 to option 6

This explain it better ?

Stuart

Forget sorry, you got me past a huge obstacle. Very much appreciated. A simple delete all on update and a loop with the changed selections and I’m ready for the next obstacle. :slight_smile:

Stuart