【发布时间】:2020-07-29 16:12:33
【问题描述】:
在author 类中有以下关联
has_many :books,
class_name :"Learning::Books",
through: :elearning,
dependent: :destroy
将 after_commit 作为,
after_commit :any_book_added?, on: :update
def any_book_added?
book = books.select { |book| book.previous_changes.key?('id') }
# book's previous_changes is always empty hash even when newly added
end
无法找到与此方法新添加的关联。这是因为class_name吗?
【问题讨论】:
-
您是否考虑过翻转这个逻辑并在
book.rb上添加after_commit以告诉拥有book的author添加了一本新书? -
有
after_add为has_many: guides.rubyonrails.org/… -
是的@yez。但我在作者模型中需要它以及其他关联更改.. JoshBrody 当我在关联中有 where 子句时它不起作用
标签: ruby-on-rails ruby associations