Skip to content

Instantly share code, notes, and snippets.

@ridiculous
Last active August 29, 2015 13:59
Show Gist options
  • Save ridiculous/10770823 to your computer and use it in GitHub Desktop.
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
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