2
我有一个教师模型和一个学生模型(教师has_many学生)。希望使用Filterrific通过他们的学生的名字搜索老师。在Rails中使用Filterrific来搜索活动记录关联
filterrific的默认功能是根据SQL查询向教师表搜索教师。
我想知道如何根据他们的关联表来搜索教师。这里是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
)
}
谢谢乔!我确实必须保留参考。另外,我不得不删除学生。 from students.first_name,students.last_name,and students.email –
我不需要包含'references'调用。 http://filterrific.clearcove.ca/pages/active_record_scope_patterns.html – eebbesen