Skip to content

Instantly share code, notes, and snippets.

@ppworks
Last active August 29, 2015 14:13
Show Gist options
  • Save ppworks/96731ed88fbe291c8ed7 to your computer and use it in GitHub Desktop.
Save ppworks/96731ed88fbe291c8ed7 to your computer and use it in GitHub Desktop.
Postモデルから、search_wordsに含まれる単語をAND検索する ref: http://qiita.com/ppworks/items/4de9e7dc324aad2c9f48
- arel_cond.and(condition)
+ arel_cond.or(condition)
scope :search, ->(search_words) {
return if search_words.empty?
post_arel = Post.arel_table[:content]
conditions = search_words.map { |word| post_arel.matches("%#{word}%") }
where_clauses = conditions.inject do |arel_cond, condition|
arel_cond.and(condition)
end
where(where_clauses)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment