我有我的車型之一以下方法:Rails的:在範圍方法
def remaining_stock
if initial_stock
initial_stock - bought_items
else
0
end
end
在控制器方面,我想拉受到有剩餘庫存用戶所擁有的所有項目大於零。因此,簡而言之,像
@remaining_items = Item.where(:user_id => current_user.id).{somehow specify that remaining stock > 0}
的方法是沿
def has_remaining_stock
remaining_stock > 0
end
線的東西,但我無法弄清楚如何將它添加到任何查詢本身,或某種(我可以用範圍:條件,但不是其他方法)
任何想法讚賞。
你肯定'initial_stock = NULL'作品?它不應該是'initial_stock IS NULL'嗎?另外,爲什麼檢查它對空att所有? – Frost