模型方法rails语法

时间:2011-11-07 16:03:09

标签: ruby-on-rails-3 model

我正在尝试编写一个方法来返回true或false,以确定某个产品是否实际存在于特定存储中。我想将存储作为参数传递,但通过控制台收到错误。什么是正确的语法?

def units_in_stock(storage)
  storage_id = Storage.find_by_id(storage)
  stocks.where("stock.storage_id = storage_id, in_stock > 0")
end

2 个答案:

答案 0 :(得分:2)

应该是:

where("stock.storage_id=? and in_stock>0", storage_id)

您也可以使用conditions

答案 1 :(得分:1)

在我看来,你传递的是一个Storage对象,所以你应该可以这样做:

def units_in_stock?(storage)
  storage.stocks.where("in_stock > 0").exists?
end