Access object within accepts_nested_attributes_for form

I have the following setup:

class Option < ActiveRecord::Base has_many :size_prices accepts_nested_attributes_for :size_prices end def new @option = Option.new @sizes = @customization.item.sizes @sizes.each do |size| @option.size_prices.build({:size_id => size.id}) end end <%= f.fields_for :size_prices do |price_form| %> I would like to do something like: <%= Size.find(price_form.size_id).name %> <%= price_form.text_field :amount %> <% end %> 

Is there a way to access the size_id of each object using a form? I would like to get the name of the size object.

+4
source share
1 answer

Yes, the .object on fields_for will give you the object that it builds for

 <%= f.fields_for :size_prices do |price_form| %> <%= price_form.object.size_id %> ... 
+3
source

All Articles