Using fields from an association (has_many) model with formtastic in rails

Posted by pduersteler on Stack Overflow See other posts from Stack Overflow or by pduersteler
Published on 2010-02-03T21:18:31Z Indexed on 2010/05/14 4:34 UTC
Read the original article Hit count: 301

I searched and tried a lot, but I can't accomplish it as I want.. so here's my problem.

class Moving < ActiveRecord::Base
  has_many :movingresources, :dependent => :destroy
  has_many :resources, :through => :movingresources
end

class Movingresource < ActiveRecord::Base
  belongs_to :moving
  belongs_to :resource
end

class Resource < ActiveRecord::Base
  has_many :movingresources
  has_many :movings, :through => :movingresources
end

Movingresources contains additional fields, like "quantity". We're working on the views for 'bill'. Thanks to formtastic to simplify the whole relationship thing by just writing

<%= form.input :workers, :as => :check_boxes %>

and i get a real nice checkbox list. But what I haven't found out so far is: How can i use the additional fields from 'movingresource', next or under each checkbox my desired fields from that model?

I saw different approaches, mainly with manually looping through an array of objects and creating the appropriate forms, using :for in a form.inputs part, or not. But none of those solutions were clean (e.g. worked for the edit view but not for new because the required objects were not built or generated and generating them caused a mess).

I want to know your solutions for this! :-)

© Stack Overflow or respective owner

Related posts about formtastic

Related posts about ruby-on-rails