Search code examples
ruby-on-railsactiverecordruby-on-rails-2

Table alias in ActiveRecord?


I have the following to find posts.

@posts = Post.find(:all, 
:select => "DISTINCT *",
:include => [:user, {:track => :artist}],
:conditions => ["user_id IN (?) AND NOT track_id = ?", users, @track.id],
:group => "track_id", 
:order => 'id desc', 
:limit => '5')

I would like to add the subselect

(SELECT COUNT(*) FROM posts P2 
    WHERE P2.user_id = P1.user_id AND P2.id > P1.id AND P2.track_id <> 34)
 <= 1

in my conditions clause, to restrict the number of posts per user.

How do I set the alias P1 to the "initial" posts table?

Using rails 2.3.11


Solution

  • You can add a from parameter:

    :from => 'posts P1',
    

    find (ActiveRecord::Base)