Take the 2-minute tour ×

I am still fairly new to Rails and fairly sure the way I'm attempting to go about this is inefficient or just plain silly, but here's what I'm trying to accomplish. I have 2 models, Cases (patient case files) and Inventories (medical transplant materials used in Cases).

class Case < ActiveRecord::Base
  has_many :inventories
  accepts_nested_attributes_for :inventories, :reject_if => :all_blank

class Inventory < ActiveRecord::Base
  belongs_to :case

Inventories are created through a separate process and the goal is to associate them with a Case through the Case form. What I am trying to do is put a table on my Case form that lists the available Inventories along with checkboxes to select the desired Inventories to associate with the Case being created. This is further complicated by the fact that I need to be able to include nested fields for a couple of attributes on each Inventory (:case_price and :case_ship_price). I had previously done this in a very roundabout way using a has_many through association and storing those attributes on the pivot table, but it involved some hacky code to capture the field inputs from params and then save them through this block:

class CasesController < ApplicationController
  def create
    @case = Case.new(params[:case])
    if @case.save
      @case.case_lineitems.each do |li|
        li.update_attributes(:price => params[:lineitem_price][li.inventory_id.to_s],
                             :shipping_cost => params[:lineitem_shipping][li.inventory_id.to_s])
      redirect_to @case
      render 'new'

This felt extremely clumsy and I was worried about problems it might cause, so I wanted to give a simple has_many, belongs_to relationship a try. However, I'm not sure if the typical <%= check_box_tag :inventory_ids, inventory.id, @case.inventories.include?(inventory), name: 'case[inventory_ids][]' %> works for that type of relationship. Here is what this section of my form looks like presently:

      <th>Serial #</th>
    <% @inventories.each do |inventory| %>
        <td><%= inventory.product.name %></td>
        <td><%= inventory.serial_num %></td>
        <%= f.fields_for :inventories, inventory do |inv| %>
            <%= inv.text_field :case_price %>
            <%= inv.text_field :case_ship_price %>
        <% end %>
    <% end %>

This results in the first checkbox defaulting to checked, and if I leave all unchecked, all of the inventories become associated upon submission. Checking only a subset results in an exception along the lines of Couldn't find Inventory with ID=206 for Case with ID=. Finally, checking all of the Inventories seems to result in the associations and nested attributes saving correctly.

How do I clean this up so that it works as desired? And if I need to go back to a has_many through relationship, is there a better way to save attributes on the pivot table on the same form that creates the row on pivot table? I'd really appreciate any help with this, as no amount of searching has gotten me out of this challenge.

share|improve this question
add comment


Your Answer



Browse other questions tagged or ask your own question.