gpt4 book ai didi

mysql - ruby on Rails 中的随机 id

转载 作者:行者123 更新时间:2023-11-29 13:30:59 26 4
gpt4 key购买 nike

我根据考试和单独的模块创建了一个测验,我想将其作为每页一个向用户显示,并且还使用此 link_to_remote 随机更改另一个模块

Controller

def answer 
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find(params[:exam_group_id])
@modules = StudentAdditionalField.find(:all)
@tot_ans = answered.count
@last_ques = (Question.count-1)
@answer = Answer.new
@ans_id = 1
@ans = Question.find_by_id(random1, :conditions => ['exam_group_id= ? ', @exam_group])
end
def next
@ans_id = 1
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find_by_id(params[:exam_group_id])
@answer = Answer.new(params[:ans])
@answer.answer = params[:answer]
@answer.exam_group_id = @exam_group.id
@answer.user_id = @user.id
passed_question = params[:passed_question]
@answer.questions_id = passed_question
@question = Question.find_by_id(passed_question)
if params[:answer] == @question.is_answer
@answer.marks = 1
else
@answer.marks = 0
end
if @answer.save
@ans_id = @ans_id + answered.count
@ans = Question.find_by_id(left_random, :conditions => ['id not in (?) && exam_group_id=?',answered, @exam_group])
render(:update) do |page|
page.replace_html 'main', :partial => 'ans', :object => @ans
end
end
end
def ans
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find(params[:exam_group_id])
answer = params[:answer]
@tot_ans = answered.count
@last_ques = (Question.count-1)
@answer = answer ? Question.find(is_answer) : nil
if @answer and @answer.correct
@correct = true
session[:correct] += 1
else
@correct = false
end
@total = session[:correct]
render(:update) do |page|
page.replace_html @ans, :partial => 'ans', :object => @ans
end
end
def final
@user = current_user
@questions = Question.count
@answers = answered.count
end

def random1
rand_id = rand(Question.count)
if rand_id == 0
rand_record = Question.first(:offset => rand(rand_id))
else
rand_record = Question.first(:conditions => [ "id = ?", rand_id])
end
end
private
def answered
@i ||= current_user.answered_questions
end

def left_random
count = Question.count - answered.count
rand = Question.first(:conditions => ['id not in (?)', answered])
end
end

我希望这些问题基于 module_id 按升序排列并且也是随机的数据库表问题是

+----+--------+-----------+-----------------------------+---------------+------+------    +------+------+
| id | ques | is_answer | student_additional_field_id | exam_group_id | ans1 | ans2 | ans3 | ans4 |
+----+--------+-----------+-----------------------------+---------------+------+------+------+------+
| 1 | df | ans1 | 1 | 1 | fd | d | df | daf |
| 2 | f | ans2 | 1 | 1 | dfa | adf | af | adf |
| 3 | adfdbd | ans4 | 1 | 1 | zxcv | dvz | zv | zcv |
| 4 | c | ans4 | 1 | 1 | aff | af | asd | adf |
+----+--------+-----------+-----------------------------+---------------+------+------+------+------+

其中student_additional_field_id是module_id 请帮帮我 提前致谢

最佳答案

据我所知,您可以使用示例方法:

def left_random
Question.where('id not in (?)', answered).sample
end

但是问题很棘手,无法得到您需要的东西

关于mysql - ruby on Rails 中的随机 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19463861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com