Gaelle
Gaelle

Reputation: 599

model method rails syntax

I'm trying to write a method to return true or false whether a product is actually in stock in a particular storage. I want to pass storage as parameter but receive an error through the console. What's the right syntax?

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

Upvotes: 0

Views: 92

Answers (2)

Adam Tanner
Adam Tanner

Reputation: 927

It looks to me that you are passing in a Storage object so you should be able to do something like this:

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

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

That should be:

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

You could also use conditions.

Upvotes: 2

Related Questions