Your has_many...through
relationship definition needs some adjustment.
# app/models/produto.rb
class Produto < ActiveRecord::Base
has_many :coletas_produtos
has_many :coletas, through: :coletas_produtos
end
Please see "has_many :through" relation for proper definition.
Then in your ColetasController#new
:
# app/controllers/coletas_controller.rb
def new
@coleta = Coleta.new
@coleta.coletas_produtos.build |cp| do
@produto = cp.build_produto
end
end
This will instantiate a new instance of Coleta
and build all the required relationships i.e. associated coletas_produtos
and produto
in your new
action.