2011-07-21 25 views
1

我有两个模型创建的认沽/后嵌套记录,与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) 

这是很奇怪!它正确地创建了关系,但实际上并没有用我发送的数据填充数据库。有任何想法吗?

+0

是什么原因发送** id与** params? – Anatoly

+0

你能复制粘贴你的控制器代码吗? – Anatoly

+0

更好的是要点它 – rubish

回答

2

我从来没有得到任何解决方案在这里工作。我所做的是通过在参数中循环来在控制器中手动执行此操作。

+0

有类似的问题,但ActiveRecord应该足够聪明来判断这一点,记错。 – wulftone

+0

我有类似的问题。我认为有一些Rails的bug。 – GreenEggs

0

我猜的错误来自:

Image.find(image.id) 

当什么也没发现这确实吐出一个例外。

简单replace_it有:

Image.find_by_id(image.id) 

它会吐出nil如果没有找到。

+0

我试过这个可能性,但它没有。如果你看看我发布的代码,你会发现我尝试从autosave_associated_records_for_images方法中记录日志。没有记录,所以看起来该函数没有被调用。 –

+0

好吧,但'autosave_associated_records_for_images'被称为'after_create'或'after_update',为什么不创建自己的'before_save'? – apneadiving

+0

我在before_save中试过。它没有工作,再次,没有写入日志。你在哪里看到autosave_associated_records_for_images作为after_create被调用?我找不到很多文档。 –

0

autosave_associated_records_for_images方法对该类包含太多的责任。解决方法是将此方法逻辑移入图片类。每个ActiveRecord类实例都有默认的回调函数,比如before_validation,before_create。我建议你先使用它们

相关问题