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

Rails `where` for time less than queries

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`.* FROM `users` WHERE (`users`.`cash_money` BETWEEN 10…
Aaron
  • 13,349
  • 11
  • 66
  • 105
24
votes
5 answers

Where can I find good AREL documentation?

I'm trying to learn as much as I can about AREL. But I'm not sure what to look at. I found some documentation on rubydoc, but it doesn't seem very good in terms of showing what are the "public API"/accessible things I can do. For example, I could…
Zabba
  • 64,285
  • 47
  • 179
  • 207
24
votes
4 answers

How to exclude an array of ids from query in Rails (using ActiveRecord)?

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] array_without_excluded_ids = Item. ??? I'm not…
CuriousYogurt
  • 836
  • 2
  • 12
  • 18
23
votes
4 answers

How to implement bulk insert in Rails 3

I need to insert a array of emails as different records into my contacts table. How can this be done. Eg: @email = ["a@b.com", "c@d.com", "e@f.com", ... ] I dont want to use. @email.each do |email| @contact = Contact.new @contact.email…
Amal Kumar S
  • 15,555
  • 19
  • 56
  • 88
23
votes
6 answers

Rails 4 query unique by single attribute

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 I want to do a query that will just…
goddamnyouryan
  • 6,854
  • 15
  • 56
  • 105
20
votes
4 answers

Is it possible to get the ActiveRecord::Relation object for an association

Do association methods, such as those defined by has_many and belongs_to utilize ActiveRecord::Relation? If so, is it possible to get the ActiveRecord::Relation object that is being used. We're all aware that Rails 3 is heavily using…
John
  • 9,254
  • 12
  • 54
  • 75
20
votes
1 answer

Arel + Rails 4.2 causing problems (bindings being lost)

We recently upgraded to Rails 4.2 from Rails 4.1 and are seeing problems with using Arel + Activerecord because we're getting this type of error: ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR: bind message supplies 0 parameters, but…
Matthew Berman
  • 8,481
  • 10
  • 49
  • 98
20
votes
2 answers

Rails 4 - how to give alias names to includes() and joins() in active record querying

How can I give an alias name for e.g. includes()? Following is given: User: active record model Student: active record model, inherits from User (STI) Teacher: active record model, inherits from User (STI) Project: active record model Here some…
phlegx
  • 2,618
  • 3
  • 35
  • 39
19
votes
1 answer

Ruby / Rails - Can I use a joined table's scope(or class method) as part of my WHERE clause?

I want to grab all the categories that contain purchaseable products. class Product < ActiveRecord::Base belongs_to :category scope :purchaseable, where(:available => true) end class Category < ActiveRecord::Base has_many :products scope…
johnnycakes
  • 2,440
  • 2
  • 28
  • 36
19
votes
1 answer

How to find the record with the maximum price?

This returns the maxium value, not the complete record: self.prices.maximum(:price_field) And currently, I find the record like this: def maximum_price self.prices.find(:first, :conditions => "price = #{self.prices.maximum(:price_field)}"…
Zabba
  • 64,285
  • 47
  • 179
  • 207
19
votes
5 answers

Nested queries in Arel

I am attempting to nest SELECT queries in Arel and/or Active Record in Rails 3 to generate the following SQL statement. SELECT sorted.* FROM (SELECT * FROM points ORDER BY points.timestamp DESC) AS sorted GROUP BY sorted.client_id An alias for the…
Schrockwell
  • 838
  • 1
  • 8
  • 25
17
votes
1 answer

How do I make an ActiveRecord scope that doesn't affect the query in Rails 3 using Arel (presumably)?

Essentially I am looking for a no-op type of relation to apply to a chain of scopes. Lets say I have a chain of scopes: Post.approved.published.all Now, for debugging purposes, I wish to make the published scope do nothing at all, so that the chain…
jakeonrails
  • 1,885
  • 15
  • 37
17
votes
2 answers

How to do "where exists" in Arel

How do you do a query that includes a "where exists" in Arel? For example on a query like this to show all the suppliers with at least one order: SELECT * FROM suppliers WHERE EXISTS (SELECT * FROM orders WHERE suppliers.supplier_id =…
Nathan Hurst
  • 1,740
  • 14
  • 22
17
votes
5 answers

Arel, Joins and Rails Queries

I've been stuck on a problem recently for a little while and found my way to Arel which looks like it should allow me to do OR's in my queries. As a starting point I needed to convert an existing Rails 3 query to Arel and that's where I've run into…
Jason
  • 397
  • 1
  • 4
  • 13
16
votes
3 answers

Join the same table twice with conditions

There are situations where ActiveRecord sets the alias table name if there are multiple joins with the same table. I'm stuck in a situation where these joins contain scopes (using 'merge'). I have a many-to-many relationship: Models table_name:…
Alex
  • 2,398
  • 1
  • 16
  • 30
1
2
3
48 49