Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 467
Jocke Avatar asked Sep 19 '26 04:09

Jocke


1 Answers

You can add a from parameter:

:from => 'posts P1',

find (ActiveRecord::Base)

like image 199
alf Avatar answered Sep 22 '26 02:09

alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!