Forum: Ruby on Rails paginate with :order AND :include?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Taylor S. (Guest)
on 2007-03-03 04:45
My two goals:
1) display last 10 posts in reverse chronological order
2) link to the author without hitting the database 2n-1 times

These work:
  def list
    @post_pages, @posts = paginate :posts, :order => "created_at DESC",
:per_page => 10
  end

  def list
    @post_pages, @posts = paginate :posts, :per_page => 10, :include =>
:person
  end

This doesn't:
  def list
    @post_pages, @posts = paginate :posts, :order => "created_at DESC",
:per_page => 10, :include => :person
  end

I get a "Column 'created_at' in order clause is ambiguous" error.  I'm
not sure but I thought it was be possible to order the posts and then
join the person table.  Otherwise I will hit the database with each
post.  Does anyone have a solution to this?  Thanks in advance!
Craig W. (Guest)
on 2007-03-03 04:55
(Received via mailing list)
On Sat, 2007-03-03 at 03:45 +0100, Taylor S. wrote:
>   def list
> I get a "Column 'created_at' in order clause is ambiguous" error.  I'm
> not sure but I thought it was be possible to order the posts and then
> join the person table.  Otherwise I will hit the database with each
> post.  Does anyone have a solution to this?  Thanks in advance!
----
def list
  @post_pages, @posts = paginate :posts,
    :order => 'posts.created_at DESC'
    :per_page => 10,
    :include => :person
end

To clarify ambiguity with two tables that have the same column name, you
clarify which table to which you are referring.

Craig
Taylor S. (Guest)
on 2007-03-03 07:23
Worked great.  Thanks!
This topic is locked and can not be replied to.