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
15
votes
2 answers

How to join a table and count records in Rails 3?

I have a Collection class which has many coins. I am trying to select collections which have more than two coins. Currently, I have no problem doing that through straight Ruby, but that's extremely inefficient. My current code: collections =…
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
14
votes
3 answers

using scope on an association

So I got this crazy idea that I wanted to apply a scope to an included association. This is what I figured out, and it seems to work just fine: class Event < ActiveRecord::Base has_many :races has_many :bad_races, :conditions =>…
jsharpe
  • 2,546
  • 3
  • 26
  • 42
14
votes
3 answers

Arel: How to cleanly join multiple conditions with OR?

In my Rails app, I loop through an array to create a list of conditions that must be joined by OR. Below is the basic flow of how I currently do so. conditions = nil set.each do |value| condition = value.to_condition conditions = conditions ?…
Matchu
  • 83,922
  • 18
  • 153
  • 160
14
votes
3 answers

Ransack: How to use existing scope?

Converting a Rails 2 application to Rails 3, I have to replace the gem searchlogic. Now, using Rails 3.2.8 with the gem Ransack I want to build a search form which uses an existing scope. Example: class Post < ActiveRecord::Base scope :year,…
Georg Ledermann
  • 2,712
  • 4
  • 31
  • 35
13
votes
3 answers

ARel mimic includes with find_by_sql

I've got a fairly complex sql query that I'm pretty sure I can't accomplish with ARel (Rails 3.0.10) Check out the link, but it has a few joins and a where exists clause, and that I'm pretty sure is too complex for ARel. My problem however is that,…
brad
  • 31,987
  • 28
  • 102
  • 155
13
votes
6 answers

Is there a way to invert an ActiveRecord::Relation query?

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? What I am looking for, should probably look like this: irb>…
Kostas
  • 8,356
  • 11
  • 47
  • 63
13
votes
6 answers

How to fetch distinct values with arel/relational algebra

I'm doing my best to bend my brain around arel and the relational algebra behind it, but how to represent a SELECT DISTINCT is consistently eluding my comprehension. Can anyone explain how to arel: SELECT DISTINCT title FROM posts; Many thanks!
jemmons
  • 18,605
  • 8
  • 55
  • 84
12
votes
2 answers

Rails 3, RSpec 2.5: Using should_receive or stub_chain with named scopes

I use Rails 3.0.4 and RSpec 2.5. In my controllers I use named scopes heavily, for example @collection = GuestbookEntry.nonreplies.bydate.inclusive.paginate( :page => params[:page], :conditions => { ... }) In my tests, I want to be able…
Jens
  • 1,386
  • 14
  • 31
12
votes
3 answers

PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

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 what columns are used in the SELECT statement, what…
Sam Johnson
  • 624
  • 1
  • 7
  • 20
12
votes
3 answers

How to use Arel::Nodes::TableAlias in an initial where statement

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…
Micha
  • 302
  • 3
  • 12
12
votes
3 answers

ActiveRecord: can't use `pluck` after `where` clause with eager-loaded associations

I have an app that has a number of Post models, each of which belongs_to a User model. When these posts are published, a PublishedPost model is created that belongs_to the relevant Post model. I'm trying to build an ActiveRecord query to find…
amd
  • 512
  • 1
  • 5
  • 13
12
votes
1 answer

Deconstructing Rails .joins & .where methods

I have two models - Banner and BannerType. Their schema looks like this: Banner # Table name: banners # # id :integer not null, primary key # name :string(255) # image :string(255) # created_at …
marcamillion
  • 32,933
  • 55
  • 189
  • 380
11
votes
1 answer

Is there a tutorial on how to use AREL or Reference document?

I am looking : for a reference document that lists what methods are available? a tutorial on how to setup and execute a query, then build up complex queries? Checked README, questions that are tagged as Arel here but most are about how to setup…
sevgun
  • 207
  • 1
  • 7
11
votes
2 answers

How do I use functions like CONCAT(), etc. in ARel?

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::Table.new(:countries), :name) => #
Seamus Abshere
  • 8,326
  • 4
  • 44
  • 61
11
votes
1 answer

Why do ARel scopes become read-only when using joins?

If you use joins in an ARel scope, the result becomes read-only (i.e. you cannot update any of the records you get back). If you would not like the result to be read-only, you just chain readonly(false) to the scope,…
Thomas Watson
  • 6,507
  • 5
  • 33
  • 43
1 2
3
48 49