我创建了一个测验,我每页显示一个项目..我想要当前用户访问的问题ID的值。在这个代码我给下一个,它呈现我想要当前用户访问的问题ID的问题
def next
@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
next_question = params[:next_question]
@question = Question.find_by_id(passed_question)
@module = Question.find_by_sql ["SELECT student_additional_field_id FROM questions WHERE id=#{passed_question}"]
student_additional_field_id = @module[0].student_additional_field_id
@questions = Question.find(:all, :conditions => [' exam_group_id=? && student_additional_field_id=? ',@exam_group,student_additional_field_id], :offset => @ans)
@ques = []
@questions.shuffle.each do |a|
@ques.push a.id unless a.id.nil?
end
a = @ques[0]
session[:ques_id] = a
@answer.modules_id = student_additional_field_id
if params[:answer] == @question.is_answer
@answer.marks = 1
else
@answer.marks = 0
end
if @answer.save
@ans = Question.find_by_id(a, :conditions => [' id not in (?) && exam_group_id=?',answered, @exam_group])
unless @ans.nil?
render(:update) do |page|
page.replace_html 'main', :partial => 'ans', :object => @ans
end
else
render(:update) do |page|
page.replace_html 'main', :partial => 'ans2'
end
end
end
end
答案 0 :(得分:0)
您的问题似乎与Rails model associations:
有关为什么我们需要模型之间的关联?因为它们很常见 代码中的操作更简单,更容易。例如,考虑一个 简单的Rails应用程序,包括客户和a的模型 订单模型。每个客户可以有很多订单
ActiveRecord关联
ActiveRecord关联允许您定义primary_key
(通常是ID),然后您可以通过foreign_key
在其他表上引用它。外键通常是一个表列,它将被称为user_id
或其他类型的ID
您可以使用以下功能关联模型:
- belongs_to的
- HAS_ONE
- 的has_many
- has_many:通过
- has_one:通过
- has_and_belongs_to_many
要回答你的问题,你显然已经current_user ID
了 - 所以如果你设法正确设置关系,你可以用这种方式调用数据:
@user = User.find(current_user.id)
@exam_group = @user.exams_groups
我会查看你的代码&把更多的信息放进去....但我认为你需要首先了解ActiveRecord关联