Ruby on rails Rails嵌套有很多关联,比如说,如何获得所有子项中的最后5个?
这么说吧Ruby on rails Rails嵌套有很多关联,比如说,如何获得所有子项中的最后5个?,ruby-on-rails,Ruby On Rails,这么说吧 Post has_many :comments 那 Comment has_many :ratings 我如何获取每个帖子的最后5个评论评分?我一直在考虑对每个帖子的评论进行循环,但这并不能解决最后5个部分 编辑:响应J。因为我似乎无法在注释字段中格式化代码 你能通过关系建立起这种关系吗?说 class Category < ActiveRecord::Base has_many :posts has_many :comments, :through => po
Post has_many :comments
那
Comment has_many :ratings
我如何获取每个帖子的最后5个评论评分?我一直在考虑对每个帖子的评论进行循环,但这并不能解决最后5个部分
编辑:响应J。因为我似乎无法在注释字段中格式化代码
你能通过关系建立起这种关系吗?说
class Category < ActiveRecord::Base
has_many :posts
has_many :comments, :through => posts
has_many :ratings, :through => comments
end
class Post < ActiveRecord::Base
belongs_to :category
has_many :comments
has_many :ratings, :through => comments
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
end
class Rating < ActiveRecord::Base
belongs_to :comment
end
类别帖子
有很多:评级,:至=>评论
结束
类Post评论
结束
类注释
我相信下面的方法可能会奏效。。。如果没有,请告诉我:]
class Post < ActiveRecord::Base
has_many :comments
has_many :ratings, :through => :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
end
class Rating < ActiveRecord::Base
# I'm assuming you have the created_at column
default_scope :order => 'created_at DESC'
end
# controller
@last_five_ratings = @post.ratings.all(:limit => 5)
class Post:评论
结束
类注释'created\u at DESC'
结束
#控制器
@最后五次评级=@post.ratings.all(:limit=>5)
我相信下面的方法可能会奏效。。。如果没有,请告诉我:]
class Post < ActiveRecord::Base
has_many :comments
has_many :ratings, :through => :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
end
class Rating < ActiveRecord::Base
# I'm assuming you have the created_at column
default_scope :order => 'created_at DESC'
end
# controller
@last_five_ratings = @post.ratings.all(:limit => 5)
class Post:评论
结束
类注释'created\u at DESC'
结束
#控制器
@最后五次评级=@post.ratings.all(:limit=>5)
您可以使用标准的ActiveRecord执行此操作:find:all,:order=>“created_at desc”,:limit=>5
。我想你可以这样包装这是一个命名的范围:
class Rating < ActiveRecord::Base
named_scope :most_recent, lambda { |n| { :conditions => [:order => 'created_at desc',
:limit => n] }
end
and in your controller:
@recent_ratings = @comment.ratings.most_recent(5)
等级[:order=>'created_at desc',
:limit=>n]}
结束
在控制器中:
@最近的评分=@comment.ratings.most\u recent(5)
您可以使用标准ActiveRecord执行此操作:find:all,:order=>“created_at desc”,:limit=>5
。我想您可以这样包装这是一个命名范围:
class Rating < ActiveRecord::Base
named_scope :most_recent, lambda { |n| { :conditions => [:order => 'created_at desc',
:limit => n] }
end
and in your controller:
@recent_ratings = @comment.ratings.most_recent(5)
等级[:order=>'created_at desc',
:limit=>n]}
结束
在控制器中:
@最近的评分=@comment.ratings.most\u recent(5)
最后,我在Rails 3下找到了我想要的东西
class Category < ActiveRecord::Base
has_many :posts
has_many :comments, :through => :posts
def ratings
Rating.category_ratings(self)
end
end
class Rating < ActiveRecord::Base
belongs_to :comment
scope :category_ratings, lambda { |c|
joins(:comment, 'INNER JOIN `posts` ON `posts`.`id` = `comments`.`post_id`').
where(:posts => {:category_id => c.id}).
select('DISTINCT `comments`.*')
}
end
类别:帖子
def评级
评级。类别_评级(自我)
结束
结束
等级{:category_id=>c.id})。
选择('DISTINCT`comments`.*')
}
结束
最后,我在Rails 3下找到了我想要的东西
class Category < ActiveRecord::Base
has_many :posts
has_many :comments, :through => :posts
def ratings
Rating.category_ratings(self)
end
end
class Rating < ActiveRecord::Base
belongs_to :comment
scope :category_ratings, lambda { |c|
joins(:comment, 'INNER JOIN `posts` ON `posts`.`id` = `comments`.`post_id`').
where(:posts => {:category_id => c.id}).
select('DISTINCT `comments`.*')
}
end
类别:帖子
def评级
评级。类别_评级(自我)
结束
结束
等级{:category_id=>c.id})。
选择('DISTINCT`comments`.*')
}
结束
要回答您的编辑,我不确定您是否可以这样做。至少我以前从来没有这样做过……我在看到您的编辑后确实尝试过这样做,但没有成功。Hrm,我想知道为什么。嵌套这种关系听起来应该更常见。我应该重新考虑我的数据模型吗?我不是说这不可能,我只是在这里做不到…你应该试着看看你得到了什么:]要回答你的编辑,我不确定你能做到。至少我以前从来没有这样做过…我在看到你的编辑后确实尝试过,但没有成功。人力资源管理,我想知道为什么。嵌套这种关系听起来应该更常见。我应该重新考虑我的数据模型吗?我不是说它是imp很可能,我在这里做不到……你应该试试看你得到了什么:]