Rails: why insert with accepts_nested_attributes_for have a empty row? -


i have problem rails when insert db: alway have row if set doc_items_attributes [] there row empty foreign key , id

class doc < activerecord::base    attr_accessible  :doc_items_attributes, :partner_info    has_many :doc_items, dependent: :destroy   accepts_nested_attributes_for :doc_items, allow_destroy:true  

model doc_items

class docitem < activerecord::base   attr_accessible :qty, :doc_id   belongs_to :doc 

data:

  params = { :doc => {   :partner_info => 'john', :doc_items_attributes => [     { :qty => '1' },     { :qty => '2' }    ]   }}    doc = doc.new()  doc.assign_attributes(params)      doc.doc_items.build  doc.save doc.doc_items.length #3 

there empty row without data in table doc_items, doc_id have value.

doc:

id partner 1 john 

doc_items:

id doc_id qty 1   1     1 2   1     2 3   1    

anybody can help?

you building new empty doc item in

doc.doc_items.build

line. empty row.

you might want use build method build doc_items.

doc = doc.create( :partner_info => "john") doc.doc_items.build(:qty => '1') doc.doc_items.build(:qty => '2')     doc.save 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -