2010-12-15 129 views
2

我有两个表。复杂的SQL语句

表A - >帖子(ID,标题,USER_ID,...,created_at,..)

tableB的 - >转播(ID,POST_ID,USER_ID, ... created_at,...)

所以,在我的Rails应用程序控制器:

@posts = Post.find(:all, :conditions => ["user_id = ? OR id IN (select post_id from reposts where user_id=?)", '1', '1'], :limit => 9) 

它工作正常,但:

我需要到ORDER BY我@posts一个组合的两列 “created_at”

任何帮助(表职位和表转播的created_at的created_at)?

回答

1

试试这个:

class Post 
    has_many :reposts 
end 


class Repost 
    belongs_to :post 
end  

Post.all(
    :include => :reposts, 
    :conditions => ["posts.user_id = ? OR reposts.user_id = ?", 1, 1], 
    :order => "posts.created_at DESC, reposts.created_at DESC 
) 
+0

我来试试吧!谢谢! – Lamp 2010-12-15 20:32:15