Select from Many to Many


#1

Here is what I have:

add_core_content.rhtml:

<% @core_contents.each do |c| %>

<option value="<%= c.id %>" <% if @resource.core_contents.include?

© %> selected=“selected”<% end %>>

   <%= c.code %>

</option>

<% end %>


admin_controller.rb:

def add_core_content

@core_contents = CoreContent.find_all_by_subject(@params["subject"],

:order => ‘level ASC’)

end

Everything works fine if I don’t have:

<% if @resource.core_contents.include? © %> selected=“selected”<% end
%>

But when that is included I get an error:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.core_contents

Extracted source (around line #3):

1:
2: <% @core_contents.each do |c| %>
3: <option value="<%= c.id %>" <% if
@resource.core_contents.include? © %> selected=“selected”<% end %>>
4: <%= c.code %>
5:
6: <% end %>

My many-to-many is core_contents_resources any my models are set up
correctly (having the habtm in both models). I know that I am getting
something with @resource.core_contents because I get information
returned (albeit unreadable,
#CoreContent:0x3321598#CoreContent:0x3321550#CoreContent:0x3321508
#CoreContent:0x33214c0)

Thanks,

Seth