在 Rails 中使用 Filterrific 搜索 Active Record Associations
Using Filterrific in Rails to search through Active Record Associations
我有一个教师模型和一个学生模型(教师 has_many 学生)。希望使用 Filterrific 按学生姓名搜索教师。
filterrific 的默认功能是根据 SQL 对教师 table 的查询来搜索教师。
我想知道如何根据他们的协会 table 搜索教师。这是 teacher.rb 中的 SQL 代码:
scope :search_query, lambda { |query|
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conditions = 3
where(
terms.map {
or_clauses = [
"LOWER(teachers.first_name) LIKE ?",
"LOWER(teachers.last_name) LIKE ?",
"LOWER(teachers.email) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
)
}
我从使用 includes
而不是 joins
的场景中获取了示例代码,因为我需要一个右外连接。在这种情况下,我必须让 Rails 知道我指的是学生 table。
我不确定您是否真的需要带有 joins
的 references
部分。试一试,我很想知道它是否有效。
scope :search_query_by_students, lambda { |query|
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conditions = 3
where(
terms.map {
or_clauses = [
"LOWER(students.first_name) LIKE ?",
"LOWER(students.last_name) LIKE ?",
"LOWER(students.email) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
).joins(:students).references(:students)
}
我有一个教师模型和一个学生模型(教师 has_many 学生)。希望使用 Filterrific 按学生姓名搜索教师。
filterrific 的默认功能是根据 SQL 对教师 table 的查询来搜索教师。
我想知道如何根据他们的协会 table 搜索教师。这是 teacher.rb 中的 SQL 代码:
scope :search_query, lambda { |query|
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conditions = 3
where(
terms.map {
or_clauses = [
"LOWER(teachers.first_name) LIKE ?",
"LOWER(teachers.last_name) LIKE ?",
"LOWER(teachers.email) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
)
}
我从使用 includes
而不是 joins
的场景中获取了示例代码,因为我需要一个右外连接。在这种情况下,我必须让 Rails 知道我指的是学生 table。
我不确定您是否真的需要带有 joins
的 references
部分。试一试,我很想知道它是否有效。
scope :search_query_by_students, lambda { |query|
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conditions = 3
where(
terms.map {
or_clauses = [
"LOWER(students.first_name) LIKE ?",
"LOWER(students.last_name) LIKE ?",
"LOWER(students.email) LIKE ?"
].join(' OR ')
"(#{ or_clauses })"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conditions }.flatten
).joins(:students).references(:students)
}