"has_many :through" and checkboxes

I have the following Models:

class Document < ActiveRecord::Base
has_many :document_topics, :dependent => true
has_many :topics, :through => :document_topics

class Topic < ActiveRecord::Base
has_many :document_topics, :dependent => true
has_many :documents, :through => :document_topics

and the following is and excert from the document controller:

def update
@document = Document.find(params[:id])
if @document.update_attributes(params[:document])
flash[:notice] = ‘Document was successfully updated.’
redirect_to :action => ‘show’, :id => @document
render :action => ‘edit’

I’m trying to do the following in a document view:

<% Topic.find(:all).each do |topic| %>
<input type=“checkbox” name=“document[topic_ids][]” value="<%=
topic.id %>" <% if @document.topics.include?(topic) %> checked=“checked”
<% end %> />
<%= topic.title %>
<% end %>

But im getting the following error when I try to update a document:

undefined method `topic_ids=’ for #Document:0x35c46f0

I thought ‘topic_ids’ was automatically generate? What am I doing wrong?

instead of “topic_ids” have you tried “topic_id”

ActiveRecord isn’t seeing a method for topic_ids because that’s not the
name of the field in your database.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs