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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • BALA MANI at Jan 5, 2012 at 9:39 am
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 4, '12 at 10:13a
activeJan 5, '12 at 9:39a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Jim Burgess: 1 post BALA MANI: 1 post

People

Translate

site design / logo © 2021 Grokbase