Trying to create a search method in my model and join conditions based on arguments passed to the method. However, nothing gets chained after the initial "where"
Controller:
Item.search(args)
Model:
def self.search(args = {})
include ActsAsTaggableOn # Tagging model for search
result = where("title LIKE ? OR description LIKE ? OR tags.name LIKE ?", "%#{args[:search]}%", "%#{args[:search]}%", "%#{args[:search]}%")
.joins("JOIN taggings ON taggings.taggable_id = items.id")
.joins("JOIN tags ON taggings.tag_id = tags.id")
# Categories
if args[:categories]
result.where(:category_id => args[:categories])
end
# Order
if args[:order] == "category"
result.joins(:categories).order("categories.title ASC")
else
result.order("title DESC")
end
# Pagination
result.paginate(:per_page => 10, :page => args[:page])
end
Even if I strip out the if block and do a chain directly afterwards, it doesn't work:
result = where(:foo => "bar")
result.order("name DESC")
... runs just the where.
Any ideas?
Thank in advance.