Missing touch option in Rails has_many relation
You can't use touch
on has_many
association, it works only with belongs_to
, that's a fact.
If I understand correctly what you want, the answers with touch:true
in the Book model won't work, because the Book object will not be updated when You change the Category model and the view will not regenerating.
So I think your solution is the best for that. (You can use also books.update_all(updated_at: Time.now)
)
As of Rails 6, there is a touch_all method available on ActiveRecord::Relation that handles this sort of thing with one query. There is a pretty good blog article on it here.