arel

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. I would like to build a default_scope that takes my permission columns into consideratio...

How do you use ARel's #as method?

If you build a projection like this: t = Arel::Table.new(:projects) ps = t.project(t[:id].as(:snark)) How do you get the result column that's named :snark? ...

Eager-loading association count with Arel (Rails 3)

Simple task: given that an article has many comments, be able to display in a long list of articles how many comments each article has. I'm trying to work out how to preload this data with Arel. The "Complex Aggregations" section of the README file seems to discuss that type of situation, but it doesn't exactly offer sample code, nor do...

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 major DBs? - I assume it will. will it be easier/harder to use with stored procedures...

How will Arel affect rails' includes() 's capabilities.

I've looked over the Arel sources, and some of the activerecord sources for Rails 3.0, but I can't seem to glean a good answer for myself as to whether Arel will be changing our ability to use includes(), when constructing queries, for the better. There are instances when one might want to modify the conditions on an activerecord :inclu...

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 subquery can be created by doing points = Table(:points) sorted = points.order('timesta...

Complex queries using Rails query language

I have a query used for statistical purposes. It breaks down the number of users that have logged-in a given number of times. User has_many installations and installation has a login_count. select total_login as 'logins', count(*) as `users` from (select u.user_id, sum(login_count) as total_login from user u ...

Finds in Rails 3 and ActiveRelation

Guys, I'm trying to understand the new arel engine in Rails 3 and I've got a question. I've got two models, User and Task class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :user end here is my routes to imply the relation: resources :users do resources :tasks end and here is my ...

rails: how do I build an active-relation scope to traverse many tables?

I have these tables and relationships: user has_many projects project has_many tasks task has_many actions I would like to build a scope that allows me to select all of the current users actions, regardless of what project or task they belong to. Thanks ...

Rails3: how to use relational algebra to replace SQL-constructions like NOT and OUTER JOIN

There are two models Post and Comment. I should get all posts, which have no comments with specific tag. How can I do this using new Rails 3 features such relational algebra (arel). SQL-solution should be something like this: SELECT `posts`.* FROM `posts` LEFT OUTER JOIN `comments` ON `posts`.`id` = `comments`.`post_id` WHERE...

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 ? conditions.or(condition) : condition end Obviously, it's not beautiful, but I still don't...

Rails 3 equivalent of complex SQL query

Given the following models: class Recipe < ActiveRecord::Base has_many :recipe_ingredients has_many :ingredients, :through => :recipe_ingredients end class RecipeIngredient < ActiveRecord::Base belongs_to :recipe belongs_to :ingredient end class Ingredient < ActiveRecord::Base end How can I perform the following SQL query us...

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! ...

Simulating has_and_belongs_to_many nested through behavior in Rails 3

So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought I'd stump it out myself with the beauty that is Arel. First, the models: class CardSet ...

Weird Rails 3 scope behavior

I've implemented the following scope in a rails 3 application: scope :popular, lambda { |l = 5| order('views desc').limit(l) } However, it seems that when you attempt to count its records directly it doesn't apply the scope filters. For example: Post.popular.size #=> 20 Checking the log, it executes the following query: SQL (0.4m...

problem: activerecord (rails3), chaining scopes with includes

In Rails3 there seems to be a problem when chaining two scopes (ActiveRelations) that each have a different include: Consider these two scopes, both of which work fine on their own: First scope: scope :global_only, lambda { |user| includes(:country) .where("countries.area_id <> ?", user.area) } Work.global_only(user) => (cut list of...

How to execute complex Arel query in Rails 3 from Model

Hi there, I'm on Rails 3,and I have a SQL query composed of a few joins that I've built up in Arel. I want to run this query from a method in one of my models, but I'm not sure of how to do this. The arel object turns out to be of type Arel::InnerJoin, and I want to retrieve an array of all objects returned from that query. Do I run Mod...

Rails3 and Arel to select using IN and subselect

I have a table called translations. (And a correspoding ActiveRecord class). This table contains the following fields id, key and value I would like to select all translations where the key matches a given query + all the translations that don't match the query, but share the key with a translation which does matches the query. The res...

[rails3] rewrite complicated query with union in arel?

I have the following models class Courier < ActiveRecord::Base has_many :coverages end class Coverage < ActiveRecord::Base belongs_to :courier belongs_to :country_code end class CountryCode < ActiveRecord::Base end and then i have the following query: # could i translate this into cleaner arel? result = Courier.find_by_...

Rails Arel selecting distinct columns

I've hit a slight block with the new scope methods (Arel 0.4.0, Rails 3.0.0.rc) Basically I have: A topics model, which has_many :comments, and a comments model (with a topic_id column) which belongs_to :topics. I'm trying to fetch a collection of "Hot Topics", i.e. the topics that were most recently commented on. Current code is as f...