【发布时间】:2018-07-11 16:15:15
【问题描述】:
在查看了包括 pg_search 在内的几个选项后,我很难找到可以帮助我了解如何组合多表、多过滤器搜索的资源。我有两个模型,profile 和 subject,它们都通过 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
【问题讨论】:
标签: ruby-on-rails ruby postgresql pg-search