sql
  • ruby-on-rails
  • ruby
  • sqlite
  • 2014-03-13 53 views 0 likes 
    0

    我想從我的數據庫中的菜單表中拉出數據。 假設x是一個餐廳的名字:我如何在SQLite中的嵌套Ruby中轉義引號?

    x = Applebee's 
    

    在我的控制,我想拉,從而滿足用戶選擇的標準(所以在這個例子特定食品,選定的卡路里是750,選定的類別是雞,所以那些被傳遞到PARAMS):

    @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')

    當餐廳的名稱沒有撇號時,它工作正常。有關如何做到這一點的任何想法?

    回答

    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 
         ) 
    
    +0

    這對我工作,你提供的第二個解決方案,謝謝! – asalgan

    0

    我會做更多的東西一樣

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

    完美,非常感謝您的幫助! – asalgan

    相關問題