如何引數化列舉?我想在方法中使用我的列舉值嗎?檢查我的 ActiveRecord 的真偽
我有方法 is_any?檢查我的 ActiveRecord
def is_any? status
album.voice_guides.all_except(params[:ids]).any?(&status?)
end
并呼叫 is_any?在這個方法中
def verify_bulk_destroy
return if album.pending?
if album.publish?
raise Api::Error::ControllerRuntimeError, :error unless is_any?
(:publish)
end
end
但它會引發錯誤
undefined method `status?' for #<Api::Admin::Albums::PremiumGuidesController:0x0000000000d278> Did you mean? status status=
uj5u.com熱心網友回復:
只需將您的is_any?方法更改為
def is_any?(status)
album.voice_guides.all_except(params[:ids]).any? { |guide| guide.status == status }
end
這將像您以前的方法一樣首先將所有記錄加載到記憶體中。
或者當您想要檢查資料庫中的條件而不將記錄加載到記憶體中時,這可能會更快,具體取決于您的需要:
def is_any?(status)
album.voice_guides.all_except(params[:ids]).where(status: status).exists?
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/484987.html
