views:

23

answers:

0

I'm having a problem with some pesky select_tags, I can get them to show data fine on the new but when it comes to edit, they don't show up, here is the code I have at the moment. I think I need to use include? but I don't know where to put it.

This is what I have at the moment for the new

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]}), :multiple => true %> 

And this is for the edit

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]},@found), :multiple => true %>         

In my controller I have this which grabs all of the codes that are related to that candidate.

@results = Candidate.all :joins => 
    "LEFT JOIN candidates_codes ON candidates.id = candidates_codes.candidate_id",
    :conditions => ["candidates.id = ?", params[:id]],
    :select => "candidates_codes.code_id"
#create an array of the candidates selected codes
@found = []
for c in @results
 @found.push(c.code_id.to_i)
end

How can I make it so I only have one select rather than having one for new and one for edit?