48 Matching Annotations
  1. Sep 2024
  2. Jul 2023
  3. Jun 2023
  4. May 2022
  5. Apr 2022
    1. join = Arel::Nodes::NamedFunction.new('json_b_array_elements', [Arel::Nodes::SqlLiteral.new("subscriptions")]) .as(Arel::Nodes::NamedFunction.new('sd', [Arel::Nodes::SqlLiteral.new("subscription_data")]).to_sql) p = e.project( Arel::Nodes::SqlLiteral.new( Arel::Nodes::Grouping.new( Arel::Nodes::InfixOperation.new('->>', sd[:subscription_data], Arel::Nodes::SqlLiteral.new("'id'"))).to_sql) << '::uuid' ).where( Arel::Nodes::InfixOperation.new('->>', sd[:subscription_data], Arel::Nodes::SqlLiteral.new("'type'").eq( Arel::Nodes::SqlLiteral.new("'Company'") ) ).and(e[:slug].eq(event_slug))) p.join_sources << Arel::Nodes::StringJoin.new( Arel::Nodes::SqlLiteral.new('CROSS JOIN LATERAL')) << join
    1. c2 = Comment.arel_table.alias s1 = Comment.arel_table. project(c2[:user_id], c2[:created_at].maximum.as('max_created_at')). from(c2).group('user_id').as('s1') puts s1.to_sql # (SELECT "comments_2"."user_id", MAX("comments_2"."created_at") AS max_created_at # FROM "comments" "comments_2" GROUP BY user_id) s1

      as() to give subselect an alias

    1. Generates the following sql in sqlite3: "SELECT \"patients\".* FROM \"patients\" INNER JOIN \"users\" ON \"users\".\"id\" = \"patients\".\"user_id\" WHERE (\"users\".\"name\" LIKE '%query%')" And the following sql in postgres (notice the ILIKE): "SELECT \"patients\".* FROM \"patients\" INNER JOIN \"users\" ON \"users\".\"id\" = \"patients\".\"user_id\" WHERE (\"users\".\"name\" ILIKE '%query%')" This allows you to join with simplicity, but still get the abstraction of the ARel matcher to your RDBMS.
    1. def self.current_table_name current_table = current_scope.arel.source.left case current_table when Arel::Table current_table.name when Arel::Nodes::TableAlias current_table.right else fail end end
  6. Feb 2022
    1. Query values including NULLs Because .where.not can be counter-intuitive regarding NULL values, you can use is_distinct_from to include them. User.where(User[:active].is_distinct_from(true))
  7. Jun 2021
  8. Feb 2021
    1. user_table = User.arel_table a = user_table.project(Arel.star, Arel.sql("1 as car_id")).take(2) b = user_table.project(Arel.star, Arel.sql("2 as car_id")).take(2) union = Arel::Nodes::UnionAll.new(a,b) User.from(Arel::Nodes::As.new(union,user_table))
  9. Mar 2020
    1. between(other)

      It looks like, if for reason you couldn't express the range as a ruby Range, it wouldn't let you pass in an array of two arbitrary values and use that (because it requires other to respond to begin and end), but you could use build your own Between node yourself like:

      Nodes::Between.new(self, left.and(right))

  10. Jan 2020
    1. Arel’s ComposabilityInternally Arel functions on AST nodes and every time an Arel method is made, the nodes in the AST will be modified accordingly. This is an important nature of how Arel works — composability, so that it’s able to build queries in a flexible way. With this abstract representation, we are able to safely add different filters and operations to the same query and even combine queries regardless of its order. Arel is able to generate the correct SQL query from it.
    1. Arel is a SQL AST (Abstract Syntax Tree-like) manager for Ruby. It allows us to write complex SQL queries in a semantic, reusable fashion. Arel is "framework framework"; it's designed to optimize object and collection modeling over database compatibility. For example, Active Record is built on top of Arel.