How i can upload Model with relationship ?

marioidival Posted by marioidival on January 18, 2013

Hello guys, can someone help me? I'm not able to update a model that is related ... I have 2 tables, Provider and Product, Provider has product_id ... I'm not able to update this table product_id vendor ...

How can I do this, info: Provider -> belongs_to: product Product -> has_many: providers

Answers

miguel.perez.leal Answered by miguel.perez.leal on January 18, 2013

The easiest way is to use accepts_nested_attributes_for in the products model.

class Product < ActiveRecord::Base
  has_many :providers
  accepts_nested_attributes_for :providers
end

class Provider < ActiveRecord::Base
  belongs_to :product
end

Please have a look on http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

and also this railscasts http://railscasts.com/episodes/196-nested-model-form-part-1

marioidival Answered by marioidival on January 18, 2013

But, this change the product directly right? i don't want change the value of product in table Product... i change the value of product in table Provider ....

miguel.perez.leal Answered by miguel.perez.leal on January 19, 2013

Can you show me some code?

Because if what you want is to change the product_id on the provider class, in the form just display a select with the different products identifiers.