Reputation: 83
Can anyone suggest me some links or help me with some ideas about how to do the boolean search in ruby on rails.
I want to use boolean operators in search field like the below one:
(A OR B) AND (NOT C)
(A OR (C AND D)) AND (NOT (F OR G))
Reference: boolean-basics-job search -- this link explain the boolean search.
But how to implement this search with ruby on rails?
Is there any way to do it in ruby on rails?
Hope this clarifies.Thanks in advance.
Upvotes: 1
Views: 563
Reputation: 1035
I am not sure you want to use SQL logic or Ruby logic. So Ruby logic
1. (A || B) && !C
2. (A || (C && D)) && !(F || G)
Rails ActiveRecord search (SQL)
Model.where('a = ? OR b = ?', a_value, b_value).where.not('c = ?', c_value)
Model.where('a = ? OR (c = ? AND d = ?)', a_value, c_value, d_value).where.not('f = ? OR g = ?', f_value, g_value)
Of course it would me much better if you create scopes for that methods.
class Model
scope :first_scope, ->(a_value, b_value) { where('a = ? OR b = ?', a_value, b_value) }
scope :second_scope, -> (c_value) { where.not('c = ?', c_value) }
end
And then
Model.first_scope(a_value, b_value).second_scope(c_value)
Upvotes: 4
Reputation: 64
Logical OR = ||
Logical AND = &&
Logical NOT = ! or not()
(A || B) && !C
(A || (C && D)) && !(F || G)
Upvotes: 0