How to create new object with existing nested attributes

Hi,

How would I go about creating a new object with an existing nested
attribute?

class Item < ActiveRecord::Base
  has_and_belongs_to_many :attachments
  accepts_nested_attributes_for :attachments, :allow_destroy => true
end

i = Item.new
=> #<Item id: nil, name: nil ... >

params = { :attachments_attributes => { "0" => {:id =>
Attachment.find(:last).id.to_s}} }
=> {:attachments_attributes=>{"0"=>{:id=>"62"}}}

i.update_attributes(params)
ActiveRecord::RecordNotFound: Couldn't find Attachment with ID=62 for
Item with ID=

I've tried using Google to find a solution, but to no avail.

Would be very grateful for any advice.

Jim

Hai!

When you are using

object.new  # you need save
object.save

when you are using

object.create #you need to save

thy this way:)