ruby on rails - Create nested records on put/post with accepts_nested_attributes_for -
i have 2 models,
landscape:
class landscape < activerecord::base has_many :images, :as => :imageable accepts_nested_attributes_for :images, :allow_destroy => true attr_accessible :id, :name, :city, :state, :zip, :gps, :images, :images_attributes, :address1 def autosave_associated_records_for_images logger.info "in autosave" images.each |image| if new_image = image.find(image.id) new_image.save! else self.images.build(image) end end end end
image:
class image < activerecord::base belongs_to :imageable, :polymorphic => true end
i'm sending post , put requests iphone update/create landscape record json. here's example post request create new landscape record , new associated image record
{ "landscape":{ "id":0, "name":"new landscape", "city":"the city", "state":"la", "zip":"71457", "images_attributes":[ { "id":0, "image_data":"image data image data image data", "is_thumbnail":1 } ], "address1":"1800 fancy road" }
}
when server receives this, spits out
activerecord::recordnotfound (couldn't find image id=0 landscape id=0):
so seems kind of circular reference issue, it's not clear how go fixing it. of interest autosave_associated_records_for_images doesn't ever seem called (also there no documentation function, had @ rails source).
i've read every post on accepts_nested_attributes_for no luck.
update
i have records creating now, can't rails pass image data image model. let me illustrate:
started post "/landscapes" 127.0.0.1 @ 2011-07-22 19:43:23 -0500 processing landscapescontroller#create json parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"im bunch of image data image data image data", "is_thumbnail"=>1}]}} sql (0.1ms) begin sql (1.6ms) describe `landscapes` arel (0.2ms) insert `landscapes` (`address1`, `city`, `gps`, `name`, `state`, `zip`, `updated_at`, `created_at`) values (null, null, null, null, null, null, '2011-07-23 00:43:23', '2011-07-23 00:43:23') sql (1.0ms) describe `images` arel (0.1ms) insert `images` (`image_caption`, `image_data`, `is_thumbnail`, `created_at`, `updated_at`, `imageable_id`, `imageable_type`) values (null, null, null, '2011-07-23 00:43:23', '2011-07-23 00:43:23', 46, 'landscape') sql (0.2ms) commit completed 201 created in 37ms (views: 2.2ms | activerecord: 14.5ms)
that's happens when don't define autosave_asociated_records_for_images. however, if define so:
def autosave_associated_records_for_images logger.info "in autosave" logger.info images.to_s end
i see following output:
started post "/landscapes" 127.0.0.1 @ 2011-07-22 19:50:57 -0500 processing landscapescontroller#create json parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"im bunch of image data image data image data", "is_thumbnail"=>1}]}} sql (0.1ms) begin sql (1.6ms) describe `landscapes` arel (0.2ms) insert `landscapes` (`address1`, `city`, `gps`, `name`, `state`, `zip`, `updated_at`, `created_at`) values (null, null, null, null, null, null, '2011-07-23 00:50:57', '2011-07-23 00:50:57') in autosave [#<image id: nil, image_caption: nil, image_data: nil, is_thumbnail: nil, created_at: nil, updated_at: nil, imageable_id: nil, imageable_type: "landscape">] sql (0.2ms) commit completed 201 created in 32ms (views: 2.2ms | activerecord: 2.1ms)
this strange! it's creating relationship properly, it's not populating database data i'm sending. ideas?
i never got either of solutions here work. did instead manually in controller looping through params.
Comments
Post a Comment