如何更新多态模型

时间:2011-07-19 05:52:40

标签: ruby-on-rails-3 polymorphic-associations

我有一个多态模型如下:

class Upload < ActiveRecord::Base  
  belongs_to :uploadable, :polymorphic => true
  has_attached_file :photo
end

class Message < ActiveRecord::Base
  has_one :upload, :as => :uploadable, :dependent => :destroy
end

用户使用带有jquery-file-upload插件的ajax hack在精美的iframe框架中上传照片。

上传的照片保存在上传表中,没有uploadable_id或uploadable_type,因为我还没有父消息。

如何在保存消息时使用消息ID和类型更新上传模型?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

@message = Message.new(params [:message]) @ message.upload = @upload @ message.save