title | category |
---|---|
Arel |
Rails |
users = Arel::Table.new(:users)
users = User.arel_table # ActiveRecord model
users[:name]
users[:id]
users.where(users[:name].eq('amy'))
# SELECT * FROM users WHERE users.name = 'amy'
users.project(users[:id])
# SELECT users.id FROM users
users.join(photos)
users.join(photos, Arel::Nodes::OuterJoin).on(users[:id].eq(photos[:user_id]))
users.take(5) # => SELECT * FROM users LIMIT 5
users.skip(4) # => SELECT * FROM users OFFSET 4
users.project(users[:age].sum) # .average .minimum .maximum
users.project(users[:id].count)
users.project(users[:id].count.as('user_count'))
users.order(users[:name])
User.arel_table
User.where(id: 1).arel