6

I have the following models

class Post < ActiveRecord::Base
  belongs_to :user, :counter_cache => true
  belongs_to :subtopic      
end


class Subtopic < ActiveRecord::Base
  belongs_to :category
  has_many :posts, :dependent => :destroy
end

class Category < ActiveRecord::Base
  has_many :posts, :through => :subtopics
  has_many :subtopics, :dependent => :destroy
end

In the posts/index page, I want to add click-able sorting to the column titles. This is fine, except that I display some information two levels deep:

  • post attributes
    • subtopic name
      • category name

I want the "Category" column of the table to be click-able, such that I can display all posts sorted by their category. Unfortunately, when I construct the "order by" part of the query, I can't seem to get anything to work for category name. I can go one level deep with

@posts = Post.includes(:user, :subtopic => :category).paginate(:page =>1, :order => 'subtopics.name ASC')

This returns the results I want. When I try going one level deeper with

@posts = Post.includes(:user, :subtopic => :category).paginate(:page =>1, :order => 'subtopic.categories.name ASC')

I get PGError: ERROR: schema "subtopic" does not exist. Pluralizing subtopic gives the same error. I feel like I'm missing something obvious. Can someone point it out?

Side note: Bad smell alert. I know. If you have suggestions on how to make this code cleaner as well, that would be very welcome. I'm aware of the Law of Demeter, but I don't see how to get the end result I want without breaking it.


UPDATE 8/3: ActiveRecord or PostgreSQL is causing another error in the controller action. The following statement is valid in the database console:

SELECT * from "posts" LEFT OUTER JOIN "users" ON "users"."id" = "posts"."user_id" LEFT OUTER JOIN "subtopics" ON "subtopics"."id" = "posts"."subtopic_id" LEFT OUTER JOIN "categories" ON "categories"."id" = "subtopics"."category_id" ORDER BY categories.name ASC LIMIT 30;

The SQL statement above is approximately what's generated from the statement below. The rails code below is valid in rails console but not in my controller action for Post#index

 @posts = Post.includes(:user, :subtopic => :category).paginate(:page =>1, :order => 'categories.category_name ASC', :conditions => ["posts.flags_count < ?", Flag.flag_threshold] )

The error: https://gist.github.com/1124135

This error occurs for :order => 'categories. ...' and :order => 'subtopics. ...' but not for :order => (column in posts). It looks like a problem with the JOIN statement that ActiveRecord generates, but the actual SQL statement looks like it should work to me.


Update 8/4: When I enable SQL logging in my environment (using a shell script), I can see that the SQL queries that ActiveRecord generates in rails console and (a controller in) rails server are different.

Specifically, on the server, AR is prepending certain columns with 'post.', such as posts.users.display_name instead of users.display_name. This only happens on the server, and only when I include in the query :order => (non-posts column) ASC/DESC

1

1 Answer 1

4

The string in your order clause is inserted into the resulting SQL pretty much as-is. So the resulting query turns out to be something like:

Select...order by subtopic.categories.name ASC

And the database has no idea what that means. You want to order by 'categories.name ASC'.

UPDATE: Ok, now will_paginate is choking on the join. I would do something like this:

@posts = Post.includes(:user, :subtopic => :category).where(["posts.flags_count < ?", Flag.flag_threshold]).order('categories.category_name ASC').paginate(:page =>1)

That should get you going. I would further clean up my controller by moving all of that finder stuff into the model, probably as a scope named 'by_category_order' or something. Then my controller code would be nice and pretty like:

@posts = Post.by_category_order.paginate(:page => params[:page])
5
  • Thanks, this was a step in the right direction. I can get this to work for a SQL statement. The Ruby equivalent works in rails console, but it breaks in the controller. I updated the post with the new error.
    – Eric Hu
    Aug 3, 2011 at 23:36
  • Hm, if I run that query with where in the console, it works fine. If I put it in my controller, Rails still chokes with the same PGError. I can even chop off the paginate portion at the end and it chokes in the same way. It's almost like there's a difference between the database in rails console and rails server...
    – Eric Hu
    Aug 4, 2011 at 3:41
  • Could be. Are both their RAILS_ENVs development?
    – JofoCodin
    Aug 4, 2011 at 3:49
  • Yep, both are development. Rails.version is the same for both at 3.0.9 as well. I've been able to narrow down the error a little further--I put some details in the post.
    – Eric Hu
    Aug 4, 2011 at 19:59
  • Actually, I think this is going out of the scope of the original question, so I'll make a new one and accept your response as the answer. Thanks for the help!
    – Eric Hu
    Aug 4, 2011 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.