如何在SQLite中嵌套Ruby中的引号?

时间:2014-03-13 02:22:54

标签: sql ruby-on-rails ruby sqlite

我试图从数据库中的Menu表中提取数据。 让我们说x是餐馆的名称:

x = Applebee's

在我的控制器中,我试图提取符合用户选择标准的特定食品(因此在此示例中,所选卡路里为750,所选类别为鸡,因此将其传递给参数) :

@items = Menu.where("calories <= '#{params[:calories]}' AND category = '#{params[:category]}' AND restaurant = \'#{x}\'")

以下是我不断收到的错误:

  

SQLite3::SQLException: near "s": syntax error: SELECT "menus".* FROM "menus" WHERE (calories <= '750' AND category = 'Chicken' AND restaurant = 'Applebee's')

当餐厅的名字没有撇号时,它可以正常工作。关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:0)

我会做更像

的事情
@items = Menu.where(
    "calories <= :calories AND category = :category AND restaurant = :restaurant", 
    {calories: params[:calories], category: params[:category], restaurant: x}
)

答案 1 :(得分:0)

试试这个

@items = Menu.scoped
@items = @items.where("calories <= (?)", params[:calories])
@items = @items.where(:category => params[:category])
@items = @items.where(:restaurant => x)

@items = Menu.where(
           "calories <= (?)", params[:calories]
         ).where(
           :category => params[:category]
         ).where(
           :restaurant => x
         )