如何通过ruby-on-rails限制对特定模型记录的操作?

更新于
2026-10-09 23:29:50
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计228个文字,预计阅读时间需要1分钟。

如何通过ruby-on-rails限制对特定模型记录的操作?

要将给定的伪代码改写成更简洁的形式,并确保不超过100个字,我们可以移除不必要的部分并直接展示核心逻辑。以下是简化后的版本:

如何通过ruby-on-rails限制对特定模型记录的操作?

rubydef workouts_on_which_i_commented comments.map(&:workout).uniqend

def comment_stream workouts_on_which_i_commented.flat_map(&:comments).sortend

如何使用以下代码将我输出的记录数限制为仅3条记录:

User.rb

def workouts_on_which_i_commented comments.map{|x|x.workout}.uniq end def comment_stream workouts_on_which_i_commented.map do |w| w.comments end.flatten.sort{|x,y| y.created_at <=> x.created_at} end

html.erb文件

<% current_user.comment_stream.each do |comment| %> ... <% end %>

更新:

我正在使用Rails 2.3.9

Rails 3:

def workouts_on_which_i_commented comments.limit(3).map{|x|x.workout}.uniq end

Rails< 3: 由于comments是一个Comment对象数组,因此您可以简单地对其进行切片:

def workouts_on_which_i_commented comments[0..2].map{|x|x.workout}.uniq end

标签:记录

本文共计228个文字,预计阅读时间需要1分钟。

如何通过ruby-on-rails限制对特定模型记录的操作?

要将给定的伪代码改写成更简洁的形式,并确保不超过100个字,我们可以移除不必要的部分并直接展示核心逻辑。以下是简化后的版本:

如何通过ruby-on-rails限制对特定模型记录的操作?

rubydef workouts_on_which_i_commented comments.map(&:workout).uniqend

def comment_stream workouts_on_which_i_commented.flat_map(&:comments).sortend

如何使用以下代码将我输出的记录数限制为仅3条记录:

User.rb

def workouts_on_which_i_commented comments.map{|x|x.workout}.uniq end def comment_stream workouts_on_which_i_commented.map do |w| w.comments end.flatten.sort{|x,y| y.created_at <=> x.created_at} end

html.erb文件

<% current_user.comment_stream.each do |comment| %> ... <% end %>

更新:

我正在使用Rails 2.3.9

Rails 3:

def workouts_on_which_i_commented comments.limit(3).map{|x|x.workout}.uniq end

Rails< 3: 由于comments是一个Comment对象数组,因此您可以简单地对其进行切片:

def workouts_on_which_i_commented comments[0..2].map{|x|x.workout}.uniq end

标签:记录