Jim Burgess
2012-Jan-04 10:13 UTC
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
--
Posted via http://www.ruby-forum.com/.
--
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
BALA MANI
2012-Jan-05 09:38 UTC
Re: How to create new object with existing nested attributes
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:)
--
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.