Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created June 3, 2017 13:19
Show Gist options
  • Save JackHowa/ae7ef66a93a9c954c8ba8e9eeaa92467 to your computer and use it in GitHub Desktop.
Save JackHowa/ae7ef66a93a9c954c8ba8e9eeaa92467 to your computer and use it in GitHub Desktop.
scratch.rb
class Card
# wrongly added has_one
# one card can have many guesses from different users
# or even during different rounds
has_many :guesses
end
class Guess
belongs_to :card
end
class Round
def increment_score
self.score += 1
end
def end_round?
guessed_correct = self.guesses.select { |guess| guess.correct == true }
guessed_correct.empty?
end
end
post '/guesses' do
user = current_user
# might as well try to assign current user
# if nothing there then @user will still be nil for
@guess = round.guesses.find_by(card: card)
# think I may be able to find by the round
# so in essence I'm still finding_by one thing
# but that the association still needs to be there
# because one card can have multiple guesses from
# different users or even by the same user in different rounds
# or by different users who aren't logged in
if @guess
else
# new guess for the user
if correct == true
# then there's the first time right plus one to score
# they got the first time guessing the new one correct
round.increment_score
end
end
end
get '/guesses/:id' do
@guess = Guess.find(params[:id])
@round = @guess.round
if round.end_round?
# redirect to stats page bc done
erb :'/rounds/show'
else
# load response page right or wrong
erb :'/guesses/show'
end
end
scratch.erb
<!-- add to form for accomodating a new user
or maybe use the session current user
think we'll be gucci though with knowing the user in the route
through current_user
-->
<form action="/guesses" method="post">
<% if signed_in? %>
<input type="hidden" name="user" value="<%= @user.try(:id) %>">
<% end %>
</form>
<!-- rounds show page -->
<h2>Congrats you finished the round</h2>
<% if signed_in? %>
<h3>Thanks <%= current_user.name %>!</h3>
<% end %>
<p>There were <%= @round.score %> question(s) answered correctly the first time</p>
<p>You started the round at <%= @round.created_at.strptime('%D') %></p>
<p>You finished the round at <%= @round.updated_at.strptime('%D') %></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment