使用accepts_nested_attributes_for在put / post上创建嵌套记录

时间:2011-07-21 17:49:54

标签: ruby-on-rails json nested-attributes autosave

我有两个型号,

风景:

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源代码。)

我已经阅读了accept_nested_attributes_for上的每篇SO帖子而没有运气。

更新

我现在正在创建记录,但是我无法通过rails将图像数据传递回Image模型。让我来说明一下:

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)

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

3 个答案:

答案 0 :(得分:2)

我从未在这里找到任何一种解决方案。我所做的是通过循环参数在控制器中手动完成。

答案 1 :(得分:0)

我猜错误来自:

Image.find(image.id)

事实上,当发现任何事情时,这会引发异常。

只需将replace_it替换为:

Image.find_by_id(image.id)

如果找不到任何内容,它会吐出nil

答案 2 :(得分:0)

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