ActiveRecord中的表别名?

时间:2011-10-02 15:53:15

标签: ruby-on-rails activerecord ruby-on-rails-2

我有以下内容来查找帖子。

@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')

我想添加子选择

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

在我的条款条款中,限制每个用户的帖子数量。

如何将别名P1设置为“初始”帖子表?

使用rails 2.3.11

1 个答案:

答案 0 :(得分:5)

您可以添加from参数:

:from => 'posts P1',

find (ActiveRecord::Base)