not*_*ere 4 ruby postgresql ruby-on-rails file callback
是否有一种方法可以dependent: :destroy在不调用关联的回调的情况下销毁Rails模型.
例:
class Administration < ActiveRecord::Base
include IdentityCache
attr_accessible :auto_sync, :response_rate_calc_state, :description,
:year, :project_id, :season, :auto_async, :synchronized_at
has_many :report_distributions
has_many :rosters, dependent: :destroy
before_destroy :delete_file
attr_accessible :file
has_attached_file :file,
path: ":class/:id_partition/:basename.:extension",
storage: :s3,
bucket: S3Config::AWS_BUCKET_MODELS,
s3_credentials: {
access_key_id: S3Config::AWS_ACCESS_KEY_ID_MODELS,
secret_access_key: S3Config::AWS_SECRET_ACCESS_KEY_MODELS
},
s3_permissions: 'authenticated-read',
s3_protocol: 'https',
s3_storage_class: :reduced_redundancy
def authenticated_url(style = nil, expires_in = 10.seconds)
file.s3_object(style).url_for(:read, secure: true, expires: expires_in).to_s
end
def delete_file
file.s3_object(nil).delete if self.file?
end
# ...
Run Code Online (Sandbox Code Playgroud)
所以我打电话的时候
Administration.find(id).destroy
Run Code Online (Sandbox Code Playgroud)
我想删除记录和附件文件,但不要调用回调进行删除 rosters
has_many :rosters, dependent: :destroy
Run Code Online (Sandbox Code Playgroud)
-
PS我不想禁用has_many :rosters, dependent: :destroy.我只需要临时禁用回调.
Nim*_*mir 12
您可以按原样保持关联,并跳过以下方法之一的回调:
1.使用delete而不是销毁,因为它不会触发回调
Administration.find(id).deleteRun Code Online (Sandbox Code Playgroud)
2.使用skip_callback方法(在此博客文章中找到):
Administration.skip_callback(:destroy, :bofore, :action_you_need_to_disable)
#then safely destroy without firing the action_you_need_to_disable callback
Administration.find(id).destroy
3.或者甚至更好,如果您已经知道何时需要跳过回调,您可以:
class Admistration < ActiveRecord::Base
has_many :rosters, dependent: :destroy
skip_callback :destroy, :before, :action_you_need_to_disable, if: -> { #conditions }
end
| 归档时间: |
|
| 查看次数: |
4670 次 |
| 最近记录: |