在ruby表中,SQL

在ruby表中,SQL,sql,ruby-on-rails,ruby,where,sql-like,Sql,Ruby On Rails,Ruby,Where,Sql Like,我正在尝试使用where子句进行筛选,并指示表 目前我正在使用这个,只有当它是完全相同的用户名时才有效: @diagnostics = Diagnostic.scoped @diagnostics = @diagnostics.includes(:user).where(tbl_users: { username: "#{params[:search]}" }) if params[:search_by] == 'by_user' 我正在尝试进行一个搜索,它可以与一个类似的应用程序一起使用,

我正在尝试使用where子句进行筛选,并指示表

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

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).where(tbl_users: { username: "#{params[:search]}" }) if params[:search_by] == 'by_user'
我正在尝试进行一个搜索,它可以与一个类似的应用程序一起使用,但它不起作用:

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).where(tbl_users: { username: "%#{params[:search]}%" }) if params[:search_by] == 'by_user'
有什么解决办法吗

谢谢。

试试这个

@diagnostics = Diagnostic.scoped

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

这就是你可以做到的:

@diagnostics = Diagnostic.scoped

@diagnostics = @diagnostics.includes(:user).all(:conditions => ['user_name LIKE ?', "%#{@q}%"])
看一看。这应该可以回答您的问题。执行的问题是。where('QUERY')不再指示表。虽然我在查询中使用变量之前尝试声明它。但它没有起作用。