代码有异味,我确定我缺少一个帮手来进行简单的更新或创建如下代码段的查询:
@user_answer = current_user.user_answers.find_by_question_id(params[:a_question])
if @user_answer.nil?
@user_answer = current_user.user_answers.build({ :answer_id => params[:an_answer] , :question_id => params[:a_question] })
@user_answer.save
else
@user_answer.update_attributes(:answer_id => params[:an_answer])
@user_answer.save
end