多表,多过滤器postgresql搜索 - rails

时间:2016-08-02 01:42:42

标签: ruby-on-rails ruby postgresql pg-search

在查看了包括pg_search在内的几个选项之后,我很难找到一个资源,可以帮助我理解如何组合多表,多过滤搜索。我有两个模型,简介和主题,两者都通过unique_id相关。我想做的是让用户从多个模型中选择多个过滤器,并返回结果。例如,我希望用户能够从配置文件模型中搜索 name 属性,并从主题模型中搜索 grade 属性,并返回一个列表那些搜索参数。我创建一个搜索过滤器没有问题,但是当我为不同类别添加更多过滤器时,其他搜索过滤器似乎没有相互通信。下面是我的一些代码。很高兴添加更多代码,只是真的不知道在哪里。我们将非常感谢您提供的任何帮助或指导。

个人资料数据库

class CreateProfiles < ActiveRecord::Migration
  def change
    create_table :profiles do |t|
      t.integer :unique_id
      t.string :name
      t.integer :age
      t.string :sex
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

主题数据库

class CreateSubjects < ActiveRecord::Migration
  def change
    create_table :subjects do |t|
      t.integer :unique_id
      t.string :subject
      t.integer :grade
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

个人资料模型

class Profile < ActiveRecord::Base

include PgSearch

  belongs_to :user

    def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
    attributes = row.to_hash
    Profile.create! attributes
   end 
  end


end

主题模型

class Subject < ActiveRecord::Base

      include PgSearch

      belongs_to :user

      def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
        attributes = row.to_hash
        Subject.create! attributes
       end 
      end

    end

1 个答案:

答案 0 :(得分:1)

请先查看Rails join table queriesRails method chaining开始。

让我们看看你可能会做些什么的一些例子。假设您要查找的个人资料名称为&#34; John&#34;以及成绩为100的科目。您可以执行以下操作:

@users = User.where(profiles: { name: "John" }, subjects: { grade: 100 }).joins(:profiles, :subjects)

请注意,仅当您根据特定值进行过滤时,才使用散列方法。现在假设你想要找到一个拥有个人资料名称的用户,这些用户名是&#34; John&#34; (例如&#34; John&#34;,&#34; John Smith&#34;或&#34; John Doe&#34;)和等级大于85,您将执行以下操作:

@users = User.where("profiles.name ILIKE ? AND subjects.grade > ?", "John%", 85).joins(:profiles, :subjects)

我记得ILIKE查询适用于Postgres,而不是MySQL。请注意,在两个语句中,您必须在查询中提及连接表名称,并且还必须调用joins方法。

现在您已了解如何连接表,现在我们可以看看如何使用参数和范围来过滤它。

class User
  scope :by_profile_name, -> (input) do
    where("profiles.name = ?", input[:profile_name]).joins(:profiles) if input[:profile_name]
  end

  scope :by_subject_grade, -> (input) do
    where("subjects.grade = ?", input[:subject_grade].to_i).joins(:subjects) if input[:subject_grade]
  end
end

然后在您的控制器中,您将拥有:

@users = User.by_subject_grade(params).by_profile_name(params)

这只是一个粗略的开始,请查看上面的链接以获取更多详细信息。

相关问题