How to update a polymorhic model
I have a polymorhpic model as follows:
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
The user uploads a photo in a fancy box iframe using the ajax hack with the jquery-file-upload plugin.
The uploaded photo is saved in the uploads table without uploadable_id or uploadable_type as I don't have the parent Message yet.
How can I update the Upload model开发者_开发问答 with the Message id and type when I save the Message?
You can try this:
@message = Message.new(params[:message]) @message.upload = @upload @message.save
精彩评论