A set of extensions to ActiveRecord Arel.
Add the gem to your Gemfile
gem 'arel_ext'
Add an initializer for the configuration:
ArelExt.install
And it's ready!
You can easily invocate custom SQL functions, like string_agg:
ArelExt::Func.string_agg(Arel::Table.new('posts')[:title], ',') # => string_agg("posts"."title", ',')
Equivalent to, in pure Arel:
args = [Arel::Table.new('posts')[:title], Arel::Nodes.build_quoted(',')]
Arel::Nodes::NamedFunction.new('string_agg', args)
You can access to arel_table columns through #[] directly from the label.
For instance:
(User[:id] == 1).to_sql # "users"."id" = 1
Equivalent to:
User.arel_table[:id].eq(1).to_sql
class User
belongs_to :posts
end
User.arel_join(:posts).to_sql # => 'INNER JOIN "posts" ON "posts"."user_id" = "users"."id"'
User.join(User.arel_join(:posts)) # => It works!
You can also alias the table name:
User.arel_join(:posts, as: 'my_posts').to_sql # => INNER JOIN "posts" "my_posts" ON "my_posts"."user_id" = "users"."id"
The gem is available as open source under the terms of the MIT License.
arel_ext is maintained by mònade srl.
We <3 open source software. Contact us for your next project!