我有两个模型创建的认沽/后嵌套记录,与accepts_nested_attributes_for
景观:
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 do |image|
if new_image = Image.find(image.id) then
new_image.save!
else
self.images.build(image)
end
end
end
end
图片:
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
我送岗位,并把请求从iPhone用json更新/创建一个横向记录。下面是一个例子POST请求创建一个新的景观记录和新的相关图像记录
{
"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"
}
}
当服务器接收到这一点,它吐出
ActiveRecord::RecordNotFound (Couldn't find Image with ID=0 for Landscape with ID=0):
因此,这似乎是某种循环参考问题,但不清楚如何去修复它。另外感兴趣的是,autosave_associated_records_for_images似乎没有被调用过(也几乎没有该函数的文档,我不得不看看rails的源代码)。
我已经阅读过关于accep_nested_attributes_for的所有SO帖子,但没有运气。
更新
我有记录现在创建,但我不能让轨图像数据传回的影像,模型。让我来举例说明:
Started POST "/landscapes" for 127.0.0.1 at 2011-07-22 19:43:23 -0500
Processing by LandscapesController#create as JSON
Parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"Im a bunch of image data image data image data", "is_thumbnail"=>1}]}}
SQL (0.1ms) BEGIN
SQL (1.6ms) describe `landscapes`
AREL (0.2ms) INSERT INTO `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 INTO `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)
这时候,我不定义autosave_asociated_records_for_images会发生什么。但是,如果我定义它像这样:
def autosave_associated_records_for_images
logger.info "in autosave"
logger.info images.to_s
end
我看到下面的输出:
Started POST "/landscapes" for 127.0.0.1 at 2011-07-22 19:50:57 -0500
Processing by LandscapesController#create as JSON
Parameters: {"landscape"=>{"name"=>"asdf", "id"=>0, "address1"=>"asdf", "city"=>"asdf", "images_attributes"=>[{"id"=>0, "image_data"=>"Im a bunch of image data image data image data", "is_thumbnail"=>1}]}}
SQL (0.1ms) BEGIN
SQL (1.6ms) describe `landscapes`
AREL (0.2ms) INSERT INTO `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)
这是很奇怪!它正确地创建了关系,但实际上并没有用我发送的数据填充数据库。有任何想法吗?
是什么原因发送** id与** params? – Anatoly
你能复制粘贴你的控制器代码吗? – Anatoly
更好的是要点它 – rubish