[英]Ruby on Rails 6 ActiveRecord associations, one model with multiple references to another model
[英]Rails 7 - ActiveRecord::Associations::Preloader.new.preload
考慮這段代碼:
# in Rails 6.1
def preload(resource, relations)
ActiveRecord::Associations::Preloader.new.preload(resource, relations)
end
所以:我想更改它以與 Rails 7 兼容,所以我寫了這個:
def preload(resource, relations)
ActiveRecord::Associations::Preloader.new(records: resource, associations: relations)
end
我做對了嗎? 因為.preload(resource, relations) 在 Rails 7 中不再存在。 如果您有任何其他建議,我非常期待
您快到了。 看起來像這樣:
ActiveRecord::Associations::Preloader.new(
records: [resource].flatten, # in case if resource is a single ApplicationRecord object
associations: relations
).call
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.