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
392
votes
5 answers

Rails where condition using NOT NIL

Using the rails 3 style how would I write the opposite of: Foo.includes(:bar).where(:bars=>{:id=>nil}) I want to find where id is NOT nil. I tried: Foo.includes(:bar).where(:bars=>{:id=>!nil}).to_sql But that returns: => "SELECT \"foos\".*…
SooDesuNe
  • 9,880
  • 10
  • 57
  • 91
225
votes
9 answers

Want to find records with no associated records in Rails

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_where? And then what about a has_many :through…
craic.com
  • 3,786
  • 5
  • 22
  • 17
203
votes
10 answers

Combine two ActiveRecord::Relation objects

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') # ActiveRecord::Relation is it possible to combine the two…
Patrick Klingemann
  • 8,884
  • 4
  • 44
  • 51
125
votes
4 answers

How to do a LIKE query in Arel and Rails?

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 becomes: SELECT * FROM USER WHERE NAME LIKE…
filsa
  • 1,646
  • 3
  • 15
  • 16
86
votes
5 answers

Subqueries in activerecord

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 ..) How can I do this using rails' 3 activerecord/ arel/…
gucki
  • 4,582
  • 7
  • 44
  • 56
86
votes
4 answers

What exactly is Arel in Rails 3.0?

I understand that it is a replacement for ActiveRecord and that it uses objects instead of queries. But... why is this better? will objects/queries be "easier" to create? will it lead to more efficient SQL queries? will it be compatible with all…
Will
  • 8,102
  • 5
  • 30
  • 32
72
votes
7 answers

How to use unscoped on associated relations in Rails3?

I have a default scope on products due to information security constraints. class Product < ActiveRecord::Base has_many :photos default_scope where('visible = 1') end In my associated Photo model, however, I also have to find products that…
crispy
  • 5,737
  • 4
  • 33
  • 45
65
votes
10 answers

ActiveRecord Arel OR condition

How can you combine 2 different conditions using logical OR instead of AND? NOTE: 2 conditions are generated as rails scopes and can't be easily changed into something like where("x or y") directly. Simple example: admins = User.where(:kind =>…
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
59
votes
6 answers

How do you scope ActiveRecord associations in Rails 3?

I have a Rails 3 project. With Rails 3 came Arel and the ability to reuse one scope to build another. I am wondering if there is a way to use scopes when defining a relationship (e.g. a "has_many"). I have records which have permission columns. …
Mario
  • 6,572
  • 3
  • 42
  • 74
49
votes
2 answers

How to order included elements in Rails 3

I have a model relationship where today has many tasks I'm trying to retrieve a user's today object, include the tasks and render them all to Json. All of this was going great until I decided I wanted to order the tasks within the today object…
royvandewater
  • 1,368
  • 2
  • 14
  • 16
44
votes
3 answers

How to override :order defined in a has_many

I have class Authors has_many :books, :order => 'name ASC' I am trying to query all the books sorted by name DESC Authors.books.order('name DESC') but the result is SELECT * FROM .... ORDER BY name ASC, name DESC and the results come back with…
Christopher
  • 453
  • 1
  • 4
  • 5
40
votes
5 answers

Best way to find a single record using ActiveRecord 3 / Arel?

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 "old" way because it continues to be useful syntactic sugar,…
John Bachir
  • 22,495
  • 29
  • 154
  • 227
32
votes
5 answers

Issue when retrieving records with empty array

I have a table of around 100 Users and I also have an array of user ids. What I wanted to do is show all users who are not a part of this array of user ids. When I do something like this User.where('id NOT IN (?)', [9, 2, 3, 4]) It successfully…
Robert
  • 596
  • 1
  • 7
  • 20
31
votes
3 answers

Rails/Arel: Selecting all records as an ActiveRecord::Relation

Using Arel in Rails - I'm looking for a way of creating an ActiveRecord::Relation that effectively results in SELECT * FROM table, which I can still manipulate further. For example, I have a model that's split up into multiple categories, and I…
Jeriko
  • 6,547
  • 4
  • 28
  • 40
29
votes
6 answers

How to find records that have duplicate data using Active Record

What is the best way to find records with duplicate values in a column using ruby and the new Activerecord?
srboisvert
  • 12,679
  • 15
  • 63
  • 87
1
2 3
48 49