我在回形針寶石中見過has_attached_file :avatar。
如何為模型創建這樣的方法?
我的目標是每當我定義屬性時,custom_method :attribute我可以運行一些活動操作,活動記錄回呼(如保存和洗掉)上的屬性資料。
uj5u.com熱心網友回復:
它只是一個類方法。
def self.custom_method(attribute)
before_save do
do_stuff_with(attribute)
end
define_method "do_stuff_with_#{attribute}" do
do_stuff_with(attribute)
end
do_other_stuff
end
您可以將其添加到您ApplicationRecord的任何地方以使其在任何地方都可用,或者將其添加到Concern/ Mixin/Module中,您可以在想要使用它時將其包含在內。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505037.html
標籤:轨道上的红宝石
下一篇:為什么我的Rails上出現“Unpermittedparameters::id,:reservation”錯誤?
