has_many through saving more than one field

Please how can I save more than one field ?

I have Product has many colors by quantity.

This works #controller: products action: new file:_form.erb     <% for color in Color.all %>       <div>         <%= check_box_tag "product[color_ids]", color.id, @product.colors.include?(color) %>           <%= color.name %>       </div>     <% end %>

but if I add one more field the value will not work.

<%= text_field_tag "stock["+color.id.to_s+"][qtd]" %>

Please anyone can help me?

My structure:

class Product < ActiveRecord::Base   has_many :stocks, :dependent => :destroy   has_many :colors, :through => :stocks end

class Color < ActiveRecord::Base   has_many :stocks   has_many :product, :through => :stocks end

class Stock < ActiveRecord::Base   belongs_to :product   belongs_to :color end

People I figure out this problem

The problem was solved with this

# models/product.rb   accepts_nested_attributes_for :stocks

# views/product/_form.erb

    <% for color in Color.all %>       <div>         <%= check_box_tag "product[stocks_attributes][color_id]", color.id, @product.colors.include?(color) %>           <%= color.name %>         <%= text_field_tag "product[stocks_attributes][qtd]" %>       </div>     <% end %>

Now I have another problem, How can I get the values from database to show in the fields for edit action ?

Any one can help me ?

Asking a question so vague - how can I get values from database - is very difficult to answer. Please give more details of what you are trying to do. Since this seems fairly basic stuff have you worked through the Rails Guides and some tutorials? railstutorial.org is good and free to use online.

Colin

Sorry about that,

I have 3 tables/models products, colors, stock, the problem is I need to save product with N colors and each color I need to tell what the quantity each color.

The form is gonna be like this:

http://postimage.org/image/2cjyus59g/

How can I save this information together ?

More especifications

Product model

class Product < ActiveRecord::Base   has_many :stocks, :dependent => :destroy   has_many :colors, :through => :stocks   belongs_to :manufacturer

  accepts_nested_attributes_for :stocks, :reject_if => lambda { |a| a[:qtd].present? }

end

Stock Model

class Stock < ActiveRecord::Base   belongs_to :product   belongs_to :color end

Color Model

class Color < ActiveRecord::Base   has_many :stocks   has_many :product, :through => :stocks end

From view _form.erb

Just the associations:

    <%= fields_for :stocks do |stock_form| %>          <% for color in Color.all %>             <%= stock_form.check_box :color_id %> <%= color.name %>             <%= stock_form.text_field :qtd %>          <% end %>     <% end %>

that's it, anyone can help me ?

Existe alguma forma que você conheça que posso preencher o valor do text_field como preencho o checkbox, algo como:

<%= text_field_tag "product[stocks_attributes] [qtd]", :value=>@product.stocks.include?(color) %>

mas o include? retorna boolean, eu queria que ele retornasse o valor da posição encontrada da cor.

Se existisse esse método já resolveria meu problema.

Anyone can Help me ?