Last active
August 29, 2015 13:57
-
-
Save delba/9756478 to your computer and use it in GitHub Desktop.
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
# As a first example, let's consider an "adjacency list", a tree represented in a table. Suppose we have a table comments, representing a threaded discussion: | |
comments = Arel::Table.new(:comments) | |
# [:id, :body, :parent_id] | |
replies = comments.alias | |
comments_with_replies = \ | |
comments.join(replies).on(replies[:parent_id].eq(comments[:id])) | |
# => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments_2.parent_id = comments.id |
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
# Suppose we have a table products with prices in different currencies. And we have a table currency_rates, of constantly changing currency rates. | |
# In Arel: | |
products = Arel::Table.new(:products) | |
# Attributes: [:id, :name, :price, :currency_id] | |
currency_rates = Arel::Table.new(:currency_rates) | |
# Attributes: [:from_id, :to_id, :date, :rate] | |
products. | |
join(:currency_rates).on(products[:currency_id].eq(currency_rates[:from_id])). | |
where(currency_rates[:to_id].eq(user_preferred_currency), currency_rates[:date].eq(Date.today)). | |
order(products[:price] * currency_rates[:rate]) |
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 ProfileGroupMemberships < Struct.new(:user, :visitor) | |
def groups | |
@groups ||= user.groups.where public.or sharedmembership | |
end | |
private | |
def public | |
table[:private].eq false | |
end | |
def shared_membership | |
table[:id].in visitor.group_id | |
end | |
def table | |
Group.arel_table | |
end | |
end |
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
users.where users[:name].eq 'amy' | |
# User.where(name: 'amy') | |
users.project users[:id] | |
# User.select :id | |
users.join(photos).on users[:id].eq photos[:user_id] | |
# | |
users.take(5) | |
# User.limit(5) | |
users.skip(4) | |
# User.offset(4) | |
users | |
.where(users[:name].eq('amy')) | |
.project(users[:id]) | |
User.where(name: 'amy').select(:id) | |
users.where(users[:name].eq('bob')).where(users[:age].lt(25)) | |
User.where(name: 'bob').where('age < ?', 25) | |
users.where(users[:name].eq('bob').or(users[:age].lt(25))) | |
User.where('name IS :name OR age < :age', name: 'bob', age: 25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment