调用某些帖子的所有评论

时间:2011-04-25 16:35:51

标签: ruby-on-rails ruby

我正在使用Rails 2.3.9应用程序,在这种情况下,帖子是workouts。我想为以下comments获取所有workouts

@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订购的那些特定训练的评论列表?

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

1 个答案:

答案 0 :(得分:0)

在您的锻炼模型中,添加:

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

然后你可以这样做:

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