Skip to content

Instantly share code, notes, and snippets.

@davidedicillo
Created January 19, 2014 19:35
Show Gist options
  • Select an option

  • Save davidedicillo/8509915 to your computer and use it in GitHub Desktop.

Select an option

Save davidedicillo/8509915 to your computer and use it in GitHub Desktop.
def create
@question = Question.find(params[:question_id])
@answer = @question.answers.create(params[:answer].permit(:option, :user_id, :question_id))
@nextquestion = Question.where.not(user_id: current_user.id).last
@index = 0
while @nextquestion.answers.where(:user_id => current_user.id).count != 0 && @index < Question.all.count do
@index = @index + 1
@nextquestion = Question.all[-@index]
end
redirect_to question_path(@nextquestion)
end
@acadavid
Copy link
Copy Markdown

There is probably a better way with just a single query, but this at least is more readable.

class Question

  def has_not_been_answered_by?(user)
    !answers.exists?(:user_id => user.id)
  end

end

@next_question = Question.where.not(:user_id => current_user.id).select { |q| q.has_not_been_answered_by?(current_user) }.last

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment