Question

I have a simple app with three models, Categories, Projects, and Categorizations:

class Category < ActiveRecord::Base
  has_many :categorizations, :dependent => :destroy
  has_many :projects, :through => :categorizations
end

class Project < ActiveRecord::Base
  has_many :categorizations, :dependent => :destroy
  has_many :categories, :through => :categorizations
  accepts_nested_attributes_for :categorizations
end

class Categorization < ActiveRecord::Base
  belongs_to :project
  belongs_to :category
end

I want to use a single form to create a new Project and associate categories with it. The portion of the form that passes through data for the Categorization model looks like this:

<div class="field">
  <strong>Categories:</strong><br>
    <%= f.fields_for :categorizations do |category| %>
      <%= collection_check_boxes :categorization, :category_ids, @categories, :id, :display_name %>
    <% end %>
</div>

...and the relevant portions of the controller look like this:

def create
    @project = Project.new(project_params)
    @categorizations = @project.categorizations.build(categorization_params)

    respond_to do |format|
      if @project.save && @categorizations.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render action: 'show', status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

def categorization_params
  params.require(:project).permit(:project_ids => [], :category_ids => [])
end

When I try to create a new record using this form it successfully creates a new Project record, but the Categorization record does not pass the category_ids through. When I look at the log it appears to be passing the category_ids as an array of strings:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"zt3KGmB20OA28P7Y21QvYjTkJvJg+gQnjOcp//XNB2I=", "project"=>{"slug"=>"new-project-31", "title"=>"New Project 31", "body"=>"Design & research", "published"=>"false"}, "categorization"=>{"category_ids"=>["1", "2", ""]}, "commit"=>"Create Project"}

INSERT INTO "categorizations" ("created_at", "project_id", "updated_at") VALUES (?, ?, ?)  [["created_at", Mon, 03 Mar 2014 00:36:09 UTC +00:00], ["project_id", 27], ["updated_at", Mon, 03 Mar 2014 00:36:09 UTC +00:00]]

Any help figuring out how to create individual Categorization records from the form would be greatly appreciated!

Was it helpful?

Solution

I'm not very sure whether this will work since you did not show the error message. But I'm guessing that it's because you did not submit the correct parameters to the build method.

Try mapping the array into a hash with something like

...build(categorization_params.map{|cat| cat})

Edit:

After discussions:

@project.categorizations.build(params[:categorization][:category_id].map{|cat| {category_id: cat}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top