arel

Group Query with Calculations on Rails 3

Rails 3 problem. I have a Foods table of foods with the following attributes: name calories (per gram) fat (per gram) carbs (per gram) protein (per gram) I then have a LoggedFoods table representing a food that has been eaten at a given time. It has the following attributes: food_id number_of_grams_eaten ate_when (datetime) So th...

complex arel query

I have a model where an article can have multiple tags (and a tag multiple articles). Article has two subclasses, product and kit. Products have a category, kits have not. How can I get all articles (both kits and products) of a certain tag (I know the tag.id) , with the product's category loaded (avoiding a n+1)? ...

arel, how to join

Given class Category < ActiveRecord::Base has_many :products, :order => 'name ASC' end Using the Rails 3 stack, how can I query for all categories that 'have' products? ...

How does one create a scope to find "Authors who have zero posts"?

Using Rails 3, this scope works as would be expected: scope :with_posts, lambda { joins(:posts). select("authors.*, count(posts.id) posts_count"). group("posts.author_id"). having("posts_count > 0") } The generated SQL is: SELECT authors.*, count(posts.id) posts_count FROM `authors` INNER J...

Arel causing infinite loop on aggregation

I have trouble with using Arel to aggregate 2 columns in the same query. When I run this, the whole server freezes for a minute, before the rails dev-server crashes. I suspect an infinite loop :). Maybe I have misunderstood the whole concept of Arel, and I would be grateful if anybody could have a look at it. The expected result of th...

What just happened to Arel and what do I do with an Arel::SelectManager ?

I'm desperately trying to make sense of Arel, mostly because I hate dealing with SQL; I was doing so well, but I've hit a wall. I've been working in Rails 3.0.0, and I'm trying to make a complex query with some math in it. The real case is rather more complex, but I've simplified a bit. In my example, I have a table with a particular ...

Rails 3 Query Interface: using associated models

I'll use the generic blog example. class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end When querying Post, how do you access its associations (i.e. :comments)? This should be the easiest thing in the world, but I haven't found any documentation on it. Even http://edgeguid...

Find your way up the polymorphic tree to one parent..

I have a simple polymorphic association #comment.rb belongs_to :commentable, :polymorphic => true has_many :comments, :as => :commentable #post.rb has_many :comments, :as => :commentable accepts_nested_attributes_for :comments, :allow_destroy => true So in IRB I can do, Post.comments, or Commen...