1

我有两个模型,

景观:

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 帖子,但都没有运气。

更新

我现在创建了记录,但我无法将图像数据传递回图像模型。让我举例说明:

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)

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

4

3 回答 3

2

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

于 2012-06-25T00:01:31.910 回答
0

我猜错误来自:

Image.find(image.id)

事实上,当什么都没有找到时,这会抛出一个异常。

只需将_it替换为:

Image.find_by_id(image.id)

nil如果什么也没找到,它会吐口水。

于 2011-07-21T20:19:14.617 回答
0

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

于 2011-07-23T02:26:11.307 回答