In my controller I have an instant variable with a where method, its part of a filter in the Search Controller.
@items = @items.where([ 'borrowable = ?', true]) if params[:top_filter] == 'borrow'
The borrowable = ?, true bit is completely wrong. What I want is to filter the @items that are borrowable by using a method in the Item's model called borrowable?.
How do I do this?
The method in the Item model is below.
def borrowable?(current_user, user, item, controller)
false
if user.invited?(current_user, user)
item.pieces.each do |piece|
if piece.available?(current_user, piece)
true
end
end
if controller == "pages"
true
end
end
end
If it’s relying on model level code, then you won’t be able to put it in a where clause. The where clause is trying to grab a list of items, but on each item you want to call a method, but you don’t have the method until your grab the items. You could potentially use a stored procedure on the DB, but just in Rails code you could do this: