Skip to content

Instantly share code, notes, and snippets.

@nowlinuxing
Last active December 22, 2015 23:38
Show Gist options
  • Save nowlinuxing/6547747 to your computer and use it in GitHub Desktop.
Save nowlinuxing/6547747 to your computer and use it in GitHub Desktop.
AR condition using arel table

Arel Table

OR condition query

admins = User.where(:kind => :admin).where_values.reduce(:and)
authors = User.where(:kind => :author).where_values.reduce(:and)

User.where(admins.or(authors))

Here, this example shows to use only ActiveRecord::Relation#where_values, but we can use other very useful methods.

User.where(admins.or(authors)).methods.grep(/values/).sort
#=> [:bind_values,
 :bind_values=,
 :eager_load_values,
 :eager_load_values=,
 :group_values,
 :group_values=,
 :having_values,
 :having_values=,
 :includes_values,
 :includes_values=,
 :instance_values,
 :joined_includes_values,
 :joins_values,
 :joins_values=,
 :order_values,
 :order_values=,
 :preload_values,
 :preload_values=,
 :select_values,
 :select_values=,
 :where_values,
 :where_values=,
 :where_values_hash]

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment