2011-04-25 51 views
0

我正在使用Rails 2.3.9应用程序,在这种情况下帖子是workouts。我正在尝试获得以下所有commentsworkouts打电话给某些帖子的所有评论

@workouts = Workout.today_only.all(:include => {:user => :memberships}, :conditions => ["workouts.public = '1' AND memberships.box_id = ?", @box.id], :order => "workouts.created_at DESC") 

我的联想是正确的:

Workout has_many :comments 
Comment belongs_to :workout 

我该如何获得在对created_at DESC下令那些specifc锻炼评论列表?

注意:today_only是一个named_scope,如果它是相关的,我可以发布代码。

+0

我建议创造那些条件更named_scopes和包括。现在你的代码在同一行上有几个抽象层次。 – kikito 2011-04-25 18:29:55

+0

是的,我同意。只需要做到这一点。 – bgadoci 2011-04-26 02:24:15

回答

0

在你的锻炼模式,添加:

has_many :comments, :order => 'created_at DESC' 

那么你可以做:

Workout.today_only.all(:include => [:comments, {:user => :memberships}], ... 
+0

之后是什么视图代码?理想情况下,我想在一个流中显示所有评论。而不是去获得每个锻炼,然后显示每个锻炼的评论。 – bgadoci 2011-04-25 21:27:48

+0

您可以:@comments = Comment.all([“workout_id IN?”,@workouts]) - 但我会将该SQL放入Comment的类方法中。 – 2011-04-26 03:28:43

相关问题