【问题标题】:Convert search feature from searchlogic to ransack rails 3将搜索功能从 searchlogic 转换为 ransack rails 3
【发布时间】:2014-09-10 15:02:01
【问题描述】:

我正在尝试从 searchlogic 转换语法并在从 rails 2.3.5 升级到 3.2.17 后使用 ransack

我的控制器中的 search_users 方法:

def search_users
  term = params[:search]

  show = Show.find(params[:show_id]) if params[:show_id]

  # Get users in show audiences
  show_user_ids = show.audiences.map(&:user_ids).flatten

  # Remove the users that have full access
    show_user_ids -= show.show_permissions.full.map(&:user_id)

    @users = User.first_name_or_last_name_or_company_like(term).id_is(show_user_ids)

    render 'shared/search_users'
 end

过时的代码是:

@users = User.first_name_or_last_name_or_company_like(term).id_is(show_user_ids)

first_name_or_last_name_or_company_like 是来自 searchlogic 的语法,由于我的应用程序上没有它,所以它未定义。

我可以使用@users = User.where(...) 并传入term = params[:search]

我将如何传递 User.where first_name 或 last_name 或 company like term ?

已尝试:@users = User.where(["first_name = :first_name or email = :email", { first_name: term, email: term }]) 及其返回用户仅使用名字进行搜索。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript ruby-on-rails ransack


    【解决方案1】:

    我得到它的工作,使用以下代码:

    @users = User.where(["first_name = :first_name or last_name = :last_name or company = :company", { first_name: term, last_name: term, company: term }])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2014-10-14
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多