Last active
August 29, 2015 13:59
-
-
Save ridiculous/10770823 to your computer and use it in GitHub Desktop.
Use Arel directly to substitute default "AND" operator with "OR" for joining where() chains
This file contains 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
class Hackery < Arel::Visitors::ToSql | |
def quack(o) | |
"WHERE #{o.wheres.map { |x| visit x, nil }.join ' OR ' }" | |
end | |
end | |
hack = Hackery.new(ActiveRecord::Base.connection) | |
condition1 = {:social_network_type => "facebook", :social_network_id => "555555"} | |
condition2 = {:social_network_type => "linkedin", :social_network_id => "555555"} | |
hack.quack(SocialUser.where(condition1).where(condition2)) | |
# output => | |
=begin | |
"WHERE ((`social_network_users`.`social_network_type` = 'facebook' AND `social_network_users`.`social_network_id` = '555555')) | |
OR ((`social_network_users`.`social_network_type` = 'linkedin' AND `social_network_users`.`social_network_id` = '555555'))" | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment