Questions tagged [arel]

Arel is a Relational Algebra for Ruby. It simplifies the generation complex of SQL queries and it adapts to various RDBMS systems.

Arel is a Relational Algebra for . It simplifies the generation complex of SQL queries and it adapts to various RDBMS systems. More info on project's page. The Arel gem is supported in framework versions 3.0 and up, however for the 2nd version of Rails the fake_arel gem shell be used.

723 questions
0
votes
1 answer

How to Group by Date on Postgres via a Rails Model

I'm using Postgres with Ruby on Rails and would like to query my posts table in away that returns all posts grouped by the day they were created_at, with each group in chronological order. A perfect example of my goal is any blog that's index lists…
SharkLaser
  • 713
  • 1
  • 10
  • 19
0
votes
1 answer

Fetch records using where clause on joined table

I have a model Voting that has a has_many relation to another model GroupVoting. The tables look like this: Voting GroupVoting - id - id - voting_id - party_id - yes [int] - no …
anonymous
  • 1,522
  • 14
  • 24
0
votes
0 answers

customize sql for has_many relation

I have two Tables: Locations, which is self-refential: int id, string name, int location_id and Nodes: int id, string name, int location_id The Relations are: class Node belongs_to :location end class Location has_many :nodes end That…
0
votes
1 answer

How do I perform a matches query on an integer attribute in Rails

If I have a users table with an id(integer) and I try building an sql query with arel, I get this. users = Arel::Table.new(:users) users.where(users[:id].matches("foo")).to_sql #=> "SELECT FROM `users` WHERE `users`.`id` LIKE 0" I need this sql…
Charles Okara
  • 91
  • 1
  • 7
0
votes
1 answer

Why is arel adding unnecessary parentheses to 'a and b or c and d or ..' query

Here's an example (which doesn't make sense, but good as an example) t = DimensionType.arel_table q = t.where( t[:label].eq('a').and(t[:label].eq('b')) .or(t[:label].eq('c').and(t[:label].eq('d'))) …
vrepsys
  • 2,143
  • 4
  • 25
  • 37
0
votes
1 answer

How to construct arel query with 'and' and 'or' on different tables?

2 models: Dimension and DimensionType DimensionType has_many :dimensions How do I construct a query like this using arel or classic ActiveRecord: select * from dimension_types dt inner join dimensions d on d.dimension_type_id=dt.id where…
vrepsys
  • 2,143
  • 4
  • 25
  • 37
0
votes
0 answers

Proper escape LIKE conditions

I want to use LIKE in my SQL request. I constructed something like this: streets = Street.where("name LIKE ?", "%#{params[:name]}%") as mentioned in Rails Guides. And I thought that it is totally OK. But I faced with following troubles: If I user…
petRUShka
  • 9,812
  • 12
  • 61
  • 95
0
votes
2 answers

Query on join table with multiple AND and OR conditions

I need to build a query in Rails that returns people that have a certain set of attributes. I simplfied my example. So here are the two tables: Table: people +----+----------+ | id | name | +----+----------+ | 1 | Person A | | 2 | Person B | |…
0
votes
0 answers

AREL query across models in Rails 4

I'm new to Rails (and SQL) and am trying to write an AREL query for a transit app. The Background: I have a few different models (Stop, Route, Trip, and StopTime). A Route has many trips, and a Trip has many stop times. This represents one route…
Kyle Chadha
  • 3,741
  • 2
  • 33
  • 42
0
votes
1 answer

Rails and Arel's where function: Can I call where on objects instead of making a call to the database?

Consider the following: budget has many objects in its another_collection. obj has many objects of the same type as object in its another_collection. budget and some_collection are already declared before the following loop they've been…
aarona
  • 35,986
  • 41
  • 138
  • 186
0
votes
1 answer

Dynamic query with multiple OR conditions

On an ActiveRecord model, I'm trying to dynamically create a query that has multiple OR conditions. ie: SELECT * from articles WHERE (name LIKE '%a%' OR desc LIKE '%a%' OR name LIKE '%b%' OR desc LIKE '%b%' OR name LIKE '%c%' OR desc…
br3nt
  • 9,017
  • 3
  • 42
  • 63
0
votes
2 answers

Pick a random record for a LARGE record set using arel / rails

I need to pick a random record for a VERY large ActiveRecord set. What's the best way to do this? I have something like this, but it still takes a long time. Model.select('id').where("id = ? AND attr = ?", self.id,…
neofetter
  • 3,004
  • 2
  • 26
  • 26
0
votes
1 answer

Filtering on a field in a has_many through association

I'm having trouble finding the best way to filter on a has_many through relationship directly with Active Record. I've found a bunch of posts here that almost address the issue but none of the answers have worked for me. Group: has_many…
errata
  • 23,596
  • 2
  • 22
  • 32
0
votes
1 answer

How to select a fixed value using Arel?

I'd like to add an extra column to an Arel select result, where the value in the column is the same on every row, so that I can add extra information about the context of the query to a Rails ActiveRecord object. So, how would I generate this SQL…
Douglas
  • 36,802
  • 9
  • 76
  • 89
0
votes
0 answers

Group by count with two columns

I would like to group on two different columns and get the count as a nested hash. User.group('role').count => gives me {"admin" => 10, "user" => 15} How do i write a query to get result like this(grouping by role and gender) {"admin" => {"male" =>…
usha
  • 28,973
  • 5
  • 72
  • 93