【问题标题】:Where like on ruby table, SQL像在红宝石表上,SQL
【发布时间】:2013-06-24 19:02:08
【问题描述】:

我正在尝试使用 where 子句进行过滤并指示表格。

目前我正在使用这个,只有当它是完全相同的用户名时才有效:

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).where(tbl_users: { username: "#{params[:search]}" }) if params[:search_by] == 'by_user'

我正在尝试搜索它可以与like一起使用,但它不起作用:

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).where(tbl_users: { username: "%#{params[:search]}%" }) if params[:search_by] == 'by_user'

有什么解决办法吗?

谢谢。

【问题讨论】:

标签: sql ruby-on-rails ruby where sql-like


【解决方案1】:

试试这个。

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).where("users.username like '%?%'", params[:search]) if params[:search_by] == 'by_user'

【讨论】:

    【解决方案2】:

    你可以这样做:

    @diagnostics = Diagnostic.scoped
    
    @diagnostics = @diagnostics.includes(:user).all(:conditions => ['user_name LIKE ?', "%#{@q}%"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      相关资源
      最近更新 更多