Last active
August 29, 2015 14:13
-
-
Save ppworks/96731ed88fbe291c8ed7 to your computer and use it in GitHub Desktop.
Postモデルから、search_wordsに含まれる単語をAND検索する ref: http://qiita.com/ppworks/items/4de9e7dc324aad2c9f48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- arel_cond.and(condition) | |
+ arel_cond.or(condition) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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