【发布时间】:2011-11-06 06:47:27
【问题描述】:
我的一个模型上有以下方法:
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
但我不知道如何将其添加到查询本身或某种引入 has_remaining_stock 的范围(我可以使用 :conditions 进行范围,但不能使用其他方法)
任何想法表示赞赏。
【问题讨论】:
标签: ruby-on-rails activerecord scope