restaurantes = Category.create(name: "Restaurantes")
carro = Category.create(name: "Carro")
hogar = Category.create(name: "Hogar")
Expense.create(category: restaurantes, date: Date.current, concept: "Hamburguesas el burral", amount: 32000)
Expense.create(category: restaurantes, date: Date.current - 4, concept: "Hamburguesas el burral", amount: 45000)
Expense.create(category: carro, date: Date.current, concept: "Gasolina", amount: 80000)
Expense.create(category: hogar, date: Date.current - 6, concept: "Lámparas", amount: 350000)
嗨,我需要搜索2个参数。 Concept是text_field,category_id是select_tag。
我正在使用sqlite3。
我需要获得相同的结果: http://localhost:3000/expenses
并为: http://localhost:3000/expenses?utf8=%E2%9C%93&concept=&category_id=
如果你想要,你可以使用我的种子:
current_user
答案 0 :(得分:0)
改变范围而不是使用OR。
class ExpensesController < ApplicationController
def index
@expenses = Expense.order("date DESC")
if params[:category_id].present?
@expenses = @expenses.where(category_id: params[:category_id])
end
if params[:concept].present?
@expenses = @expenses.where("concept LIKE ?", "%#{params[:concept]}%")
end
end
end