views:

461

answers:

1

Once again, I have my form_for for my Order model, which has_many :services, :through => :requests. Here's the layout of how I have my relationships mapped:

 __________                               _________
| Customer |                             | Utility |
 ----------                               ---------
   ||   ^                                     /\
   ||   |                                     ||
   \/   |                                     /\
   _______           _________             _________
  | Order | <=====< | Request |  >=====>  | Service | 
   -------           ---------             ---------
                                              \/
                                              ||
                                              \/
                                           _________  
                                          | Company |
                                           ---------

Where:

--->                    = belongs_to
===>                    = has_many
<==< join model >==>    = has_many, :through

On my Order form, I want to have an array of checkboxes that represent the services available, such that even though the checkboxes are labelled by the Company and categorized by Utility, the Order ends up with the Service association when the order is complete (because that's really what the customer is ordering: a Company to provide a Utility, which is a Service).

How do I accomplish this in my form?


form view:

- form_for @order do |order_form| 
  -# order form inputs, etc.
  - order_form.fields_for :customer do |customer_form| 
    -# customer form inputs
  - order_form.fields_for :services do |services_form| 
    %dl
      - @services.each do |service_name, services| 
        %dt= service_name
        - services.each do |service_item| 
          %dd
            =# check_box ??????????
            =# label ??????????, Company.find(service_item.company_id).name
  %p= order_form.submit 'Create Order'

Where:

@services = Service.all.to_set.classify { |service_item| Utility.find(service_item.utility_id).name }
A: 

There is a Railscast on HABTM checkboxes -- it's an oldie but goodie. I'm pretty sure it should still work even with a join model.

ScottJ
If only all of life's problems could be solved by Ryan Bates's Railscasts... that would be the life. Thanks; this is exactly what I needed!
neezer