0

I would like to execute the following code to perform a search in my controller:

experts_controller.erb

@users = User.text_search(params[:query]).has_skill_categories(params[:skills]).has_marketing_assets(params[:marketing_platforms])

When I run the app I get the following error:

PG::SyntaxError: ERROR:  subquery has too many columns
LINE 1: ...S pg_search_rank FROM "users"  WHERE "users"."id" IN (SELECT...

When I remove the text_search method which uses pg_search I get it works as planned:

@users = User.has_skill_categories(params[:skills]).has_marketing_assets(params[:marketing_platforms])

Can you tell me why I am getting the error and how to fix it?

Here is my code.

User model - user.erb

include PgSearch
  pg_search_scope :search, against: [:first_name, :last_name, :company, :description, :job_title, :website, :email, :country, :city, :state],
                  using: {tsearch: {dictionary: 'english'}}

  def self.text_search(query)
    if query.present?
      search(query)
    else
      User.all
    end
  end


  def self.has_marketing_assets(platforms)

    if platforms.present?
      where{users.id.in(User.joins{marketing_assets}.where((['marketing_assets.marketing_platform_id = ?' ] * platforms.count).join(' OR '), *platforms).group{users.id}.having{{marketing_assets => {COUNT(DISTINCT(marketing_assets.marketing_platform_id)) => platforms.count }}})}
    else
      User.all
    end
  end


  def self.has_skill_categories(skills)

    if skills.present?

      where{users.id.in(User.joins{my_skills}.where((['my_skills.skill_id = ?' ] * skills.count).join(' OR '), *skills).group{users.id}.having{{my_skills => {COUNT(DISTINCT(my_skills.skill_id)) => skills.count }}})}
    else
      User.all
    end
  end
miggitty
  • 47
  • 2
  • 9

0 Answers0