arel Questions

4

I got stuck on this and for sure it's easy, but I just cannot find the solution in the docs. I have some tree structure and the child where clause that I have to filter with an "exists" sub query:...
Dissatisfaction asked 7/5, 2014 at 8:26

5

Solved

Setup Rails' where method can take a range in a hash to generate a query that will search for a value that is within the range. For example: User.where(cash_money: 10..1000) #=> SELECT `users`...
Autocratic asked 29/5, 2014 at 14:51

3

Solved

Is there a straight-forward way to negate an entire where expression using ActiveRecord/ARel? It seems that where.not(), when given a hash of arguments, negates each expression individually, as opp...
Stenography asked 12/12, 2017 at 16:36

4

Solved

I'm trying to query the equivalent of this sql snippet using arel: WHERE (("participants"."accepted" = 'f' AND "participants"."contact_id" = 1) OR "participants"."id" IS NULL) So I want (accep...
Raney asked 10/11, 2011 at 16:56

4

Solved

I want to do something like: SELECT * FROM USER WHERE NAME LIKE '%Smith%'; My attempt in Arel: # params[:query] = 'Smith' User.where("name like '%?%'", params[:query]).to_sql However, this be...
Conchaconchie asked 13/12, 2010 at 15:45

10

Solved

Suppose I have the following two objects: first_name_relation = User.where(:first_name => 'Tobias') # ActiveRecord::Relation last_name_relation = User.where(:last_name => 'Fünke') # ActiveRe...
Lovmilla asked 2/3, 2012 at 21:46

6

Let's say we have the following: irb> Post.where(:hidden => true).to_sql => "SELECT `posts`.* FROM `posts` WHERE posts.hidden = 1" Could we somehow get an inverted SQL query out of it? ...
Dorsey asked 14/3, 2011 at 18:5

5

Solved

Where I used to do this: Foo.find_by_bar('a-value') I can now do this: Foo.where(:bar => 'a-value').limit(1).first Is this recommended? Is this the best way? Should I continue to use the "...
Zitvaa asked 13/6, 2011 at 3:38

2

Solved

Is there a way to have ARel write (sanitized, possibly aliased, etc.) column names into CONCAT() and other SQL functions? Here's how to do it with AVG()... ?> name = Arel::Attribute.new(Arel::...
Tote asked 16/2, 2012 at 2:34

3

Solved

I have a long chain of associations, joins, order by, etc., that is ultimately selecting from one of my rails models. At the end of the day I need the results to be unique and sorted. I don't care ...
Shih asked 16/3, 2017 at 22:58

9

Solved

Consider a simple association... class Person has_many :friends end class Friend belongs_to :person end What is the cleanest way to get all persons that have NO friends in ARel and/or meta_wh...
Gravamen asked 15/3, 2011 at 23:47

1

Rails 6.0.2.1 ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux] my ArticleDir class has 2 scopes: scope :active, -> { where(active: true).where(expired_on: nil) } scope :fetched_state,...
Outwash asked 5/4, 2020 at 14:5

4

Solved

I would like to perform an ActiveRecord query that returns all records except those records that have certain ids. The ids I would like excluded are stored in an array. So: ids_to_exclude = [1,2,3...
Stinson asked 3/1, 2011 at 0:49

3

Solved

I have a few massive SQL request involving join across various models in my rails application. A single request can involve 6 to 10 tables. To run the request faster I want to use sub-queries in t...
Sletten asked 3/4, 2013 at 13:47

1

Solved

I'm trying to clean up an ActiveRecord code written with MySQL case statements using ARel. I know Arel is able to handle case statements, but not sure how to use it. So I have an orders table with...
Pompea asked 26/2, 2020 at 10:2

2

So, let's get straight: I'm struggling to days do convert this SQL Statement: select * from ( select distinct on (program_id) editions.*, programs.* from editions inner join programs on edition...
Thais asked 26/5, 2017 at 23:44

2

I am trying to find if there exists a more Rails-y way to generate the following query # The purpose of this query is to select Users who have not updated # their user record. SELECT `users`.* FR...
Ariannaarianne asked 29/11, 2017 at 16:18

2

Solved

Hows do one obtain the UNION operation result in Rails. Given I have the following SQL statement SELECT "sip_trunks".* FROM "sip_trunks" WHERE "sip_trunks"."default" = t LIMIT 1 UNION ALL SELECT ...
Squires asked 14/8, 2019 at 8:45

2

Solved

Using model concerns which include scopes, what is the best way to write these knowing that nested and/or self-referencing queries are likely? In one of my concerns, I have scopes similar to these...
Beaudoin asked 23/2, 2015 at 22:54

6

Solved

So this is more of an arel question than anything but here's what I am trying to do. I have three objects lets say, called Items <Item id: 1, name: 'Book'> <Item id: 2, name: 'Car'> &...
Anaxagoras asked 5/8, 2013 at 3:45

5

With SQL I can easily do sub-queries like this User.where(:id => Account.where(..).select(:user_id)) This produces: SELECT * FROM users WHERE id IN (SELECT user_id FROM accounts WHERE ..) ...
Swoon asked 30/3, 2011 at 7:47

2

Solved

I have a simple model class User has_many :logs class Logs related in the usual way through the foreign key logs.user_id. I'm trying to do the following using Arel and according to the Arel d...
Sackett asked 23/9, 2011 at 12:12

2

So I put quite a bit of time to write this query, and then found out the hard way that this is returning an array rather than an activerecord relation. DOH. This wouldn't be a problem, but i need t...
Misology asked 12/12, 2018 at 0:37

4

Solved

Suppose I have the following models: class Post < ActiveRecord::Base has_many :authors class Author < ActiveRecord::Base belongs_to :post And suppose the Author model has an attribute, ...

6

Solved

What is the best way to find records with duplicate values in a column using ruby and the new Activerecord?
Decalcify asked 24/2, 2011 at 14:42

© 2022 - 2024 — McMap. All rights reserved.