2017-04-11 4 views
2

私はactiveadminをRails 5で使用しています。以下のアソシエーションがあります。ActiveAdminアソシエーションを使用して変更がない場合、関連モデルのコールバックをトリガーする方法

class Manager < ApplicationRecord 
    has_many :employees 
    accepts_nested_attributes_for :employees, :allow_destroy => true 
end 

class Employee < ApplicationRecord 
    belongs_to :manager 

    after_save :do_something 
end 

そして、私のフォームは次のようになります。管理者の編集]ページで

tabs do 
    tab 'Details' do 
    f.inputs do 
     input :name 
     input :current_address 
     input :email_address 
    end 
    end 

    tab 'Employees' do 
    f.has_many :employees, heading: false, allow_destroy: true do |employee| 
     input :name 
     input :experience 
     input :email_address 
    end 
    end 
end 

私はマネージャーの詳細ではない従業員の詳細を編集した場合、従業員のモデルにafter_saveコールバックがトリガーされません、それは意味をなさない。しかし、従業員の詳細が変更されていないのに、従業員モデルのコールバックをトリガーする方法がありますか?

答えて

0

従業員モデルのコールバックをトリガーするManagerモデルでafter_saveメソッドを使用する方法はありますか?

class Manager < ApplicationRecord 
    has_many :employees 
    accepts_nested_attributes_for :employees, :allow_destroy => true 

    after_save :update_employees 

    def update_employees 
    employees.each {|m| m.update_attributes()} 
    end 

end 
+0

問題を解決します。しかし、私は従業員のモデルのマネージャーの詳細after_saveと一緒に従業員を編集する場合は、マネージャの編集ページで二回呼び出されます。アクティブな管理者の作業の仕方は、関連するレコードと親レコードを最初に保存することです。他のアイデア? – Dinesh

関連する問題