简体   繁体   English

更改虚拟属性时如何关闭回调?

[英]How to turn off callbacks when changing virtual attribute?

My model has an 'url' virtual parameter with a big amount of data at the link. 我的模型有一个“ url”虚拟参数,链接上有大量数据。 The same model has a paperclip attachment: so when a new instance is created, it can contain the data for attachment or url-string from where the object should download it. 相同的模型具有回形针附件:因此,在创建新实例时,它可以包含对象应从中下载附件的数据或url字符串。 I want to encapsulate all the checking if it is url-based data or the raw one from controller: 我想封装所有检查是否是基于url的数据或来自控制器的原始检查:

@music = @order.build_music(params[:soundtrack])
if @music.save
    format.html { redirect_to edit_order_path(@order) }
    format.js 
else
    format.html {render :action => 'new'}
    format.js
end

I want My music model check if there is a url virtual parameter is set and if it so download it asynchronously: 我想要我的音乐模型检查是否设置了url虚拟参数,并且是否异步下载:

has_attached_file :soundfile

after_save :check_if_url

def self.downloadSoundtrack(id, url)
    find(id).update_column(:soundfile, open(url))
end

private
    def check_if_url
        if self.url.present?
            Soundtrack.delay.downloadSoundtrack(self.id, self.url)
        end
    end

but update_column does not work with virtual parameters, and I can't use save 'cuz it will fire after_save callback recusivelly. 但是update_column不适用于虚拟参数,并且我不能使用save'cuz它将回响地触发after_save回调。 Plz, help, guys! 请帮忙,伙计们!

试试这个,它不会触发回调

Model.where(id: id).update_all(soundfile: open(url))

API文档 :通过传递validate: false可以跳过save的验证过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM