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 ?


On Oct 12, 9:37 am, Colin Law <[email protected]> wrote:
> On 12 October 2011 13:24, Farah <[email protected]> wrote:
>
> > Now I have another problem, How can I get the values from database to
> > show in the fields for edit action ?
>
> 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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to