我正在构建 Rails 测验应用程序。我有包含类别、问题输入和答案的模型问题。 所以我需要从表问题-列类别中随机选择7个类别名称并将其显示在 View 中。 之后,我将从每个选定的类别中对 Question_input 执行相同的操作(随机选择 10 个问题)。 所以我被困在这里,我需要一些帮助。
下面是我的模型。
class Question < ActiveRecord::Base
CATEGORIES = %w(history geography art literature comics movie music culture religion mithology sport technology science politics)
validates :category, inclusion: { in: CATEGORIES }, presence: true
validates :question_input, presence: true
validates :answer, presence: true
end
欢迎任何帮助!
最佳答案
CATEGORIES.sample 7
请参阅有关如何使用的文档:http://ruby-doc.org/core-1.9.3/Array.html#method-i-sample
关于mysql - Rails 从表列中随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35440075/