[英]Rails Serializer Return Previous Data If Statement Exist
我在 Rails api 序列化器上有一些問題。 有一個更新 object 的 if 語句,當我更新 object 時,它會返回以前的數據。 但是如果我刪除 if 語句,那么它會返回 object 的更新版本。
if @store.schedule_relations.present?
params[:schedule_params].each do |schedule_param|
@store.btc_box_merchant_lku_schedule_relations.find_by(day: schedule_param[:day]).update_attributes!(is_open: schedule_param[:is_open], opening_time: schedule_param[:opening_time], closing_time: schedule_param[:closing_time])
end
end
商店 Model 看起來像這樣:
has_many: :schedules
計划 Model 如下所示:
belongs_to: :store
如果我刪除 if 語句,它確實可以在不重新加載方法的情況下工作。 我可以通過重新加載來解決,我想知道它為什么會這樣。 任何想法?
當在 Rails 上的 Ruby 中調用@store.schedule_relations
之類的關系時,該查詢的結果將被緩存,直到當前請求結束。 因此,當您更改基礎數據時,這些更改將不會顯示在隨后的@store.schedule_relations
調用中。
要清除緩存並使用您需要調用的更新數據強制重新加載相關記錄
@store.schedule_relations.reload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.