Ruby 查看 class 创建
Ruby Review class creation
您在下方看到的评论 class 代表用户为产品提交的评论。在代码的其他地方,Review.recent 是用 product_id 调用的,它只是代表单个产品的唯一数字。填写代码,使其按预期运行!
Review.recent - 此函数应 return 具有指定 product_id.
的 5 条最新评论(按 submit_time 排序)
<=> - 这个特殊的 Ruby 函数在比较两个对象进行排序时被调用。它 returns 1、0 或 +1 取决于对象是小于、等于还是大于另一个对象。您需要按 submit_time 对项目进行排序,以便最先显示最近的项目。
set_submit_time - 在创建评论之前调用此函数。我们可以使用 Ruby 的时间 class 将 submit_time 设置为现在,以便我们知道评论的创建时间。
我是 ruby 的新手,我需要这个代码来完成我非常重要的工作,所以请帮助我如何完成它!
class Review < ApplicationRecord
# Every Review has a product_id and submit time
attr_accessor :product_id
attr_accessor :submit_time
# Before the new record is created, we'll call the :set_submit_time method
before_create :set_submit_time
def self.recent(product_id)
# Return only the 5 newest results for this product
# Reference: https://ruby-doc.org/core-2.4.2/Enumerable.html
Review.all
end
def <=>(other_review)
# Implement the comparison function for sorting
# Reference: http://ruby-doc.org/core-2.4.2/Comparable.html
end
private
def set_submit_time
# Set the submit_time
# Reference: https://ruby-doc.org/core-2.2.4/Time.html
end
end
self.recent
这要求您按 submit_time
和 return 前 5 个结果进行排序。
要执行排序,请参阅:https://apidock.com/rails/ActiveRecord/QueryMethods/order
要执行限制,请参阅:https://apidock.com/rails/ActiveRecord/QueryMethods/limit
如果您仍然卡在这个问题上,请向我们展示您的尝试。
<=>
如果单击您提供的评论中的 link (http://ruby-doc.org/core-2.4.2/Comparable.html),解决方案与该示例几乎相同。
如果您仍然卡在这个问题上,请向我们展示您的尝试。
set_submit_time
值得快速浏览一下:https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html - 了解回调的含义。基本上,每当创建新记录时都会自动调用此方法。 (根据相当不言自明的名称,您可能已经猜到这一点:before_create
!)
同样,该页面上的第一个示例与您的场景几乎相同。您可以使用 Time.now
获取当前时间。
如果您仍然卡在这个问题上,请向我们展示您的尝试。
您在下方看到的评论 class 代表用户为产品提交的评论。在代码的其他地方,Review.recent 是用 product_id 调用的,它只是代表单个产品的唯一数字。填写代码,使其按预期运行!
Review.recent - 此函数应 return 具有指定 product_id.
的 5 条最新评论(按 submit_time 排序)<=> - 这个特殊的 Ruby 函数在比较两个对象进行排序时被调用。它 returns 1、0 或 +1 取决于对象是小于、等于还是大于另一个对象。您需要按 submit_time 对项目进行排序,以便最先显示最近的项目。
set_submit_time - 在创建评论之前调用此函数。我们可以使用 Ruby 的时间 class 将 submit_time 设置为现在,以便我们知道评论的创建时间。
我是 ruby 的新手,我需要这个代码来完成我非常重要的工作,所以请帮助我如何完成它!
class Review < ApplicationRecord
# Every Review has a product_id and submit time
attr_accessor :product_id
attr_accessor :submit_time
# Before the new record is created, we'll call the :set_submit_time method
before_create :set_submit_time
def self.recent(product_id)
# Return only the 5 newest results for this product
# Reference: https://ruby-doc.org/core-2.4.2/Enumerable.html
Review.all
end
def <=>(other_review)
# Implement the comparison function for sorting
# Reference: http://ruby-doc.org/core-2.4.2/Comparable.html
end
private
def set_submit_time
# Set the submit_time
# Reference: https://ruby-doc.org/core-2.2.4/Time.html
end
end
self.recent
这要求您按 submit_time
和 return 前 5 个结果进行排序。
要执行排序,请参阅:https://apidock.com/rails/ActiveRecord/QueryMethods/order
要执行限制,请参阅:https://apidock.com/rails/ActiveRecord/QueryMethods/limit
如果您仍然卡在这个问题上,请向我们展示您的尝试。
<=>
如果单击您提供的评论中的 link (http://ruby-doc.org/core-2.4.2/Comparable.html),解决方案与该示例几乎相同。
如果您仍然卡在这个问题上,请向我们展示您的尝试。
set_submit_time
值得快速浏览一下:https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html - 了解回调的含义。基本上,每当创建新记录时都会自动调用此方法。 (根据相当不言自明的名称,您可能已经猜到这一点:before_create
!)
同样,该页面上的第一个示例与您的场景几乎相同。您可以使用 Time.now
获取当前时间。
如果您仍然卡在这个问题上,请向我们展示您的尝试。