diff --git a/db/schema.rb b/db/schema.rb index fd4484e..887f52a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do +ActiveRecord::Schema[7.2].define(version: 2025_03_22_172051) do create_table "candidates", force: :cascade do |t| t.integer "vote_id" t.string "name" @@ -26,8 +26,10 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do t.integer "value" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "vote_id", null: false t.index ["candidate_id"], name: "index_ratings_on_candidate_id" t.index ["user_id"], name: "index_ratings_on_user_id" + t.index ["vote_id"], name: "index_ratings_on_vote_id" end create_table "users", force: :cascade do |t| @@ -45,4 +47,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do t.datetime "created_at", null: false t.datetime "updated_at", null: false end + + add_foreign_key "ratings", "votes" end diff --git a/views/votes_show.erb b/views/votes_show.erb index 052e20f..206740b 100644 --- a/views/votes_show.erb +++ b/views/votes_show.erb @@ -13,13 +13,13 @@

<%= candidate.name %>

<%= candidate.description %>

    - checked<% end %>> - checked<% end %>> - checked<% end %>> - checked<% end %>> - checked<% end %>> - checked<% end %>> - checked<% end %>> + checked<% end %>> + checked<% end %>> + checked<% end %>> + checked<% end %>> + checked<% end %>> + checked<% end %>> + checked<% end %>>
<% end %> @@ -27,6 +27,27 @@ +

All ratings

+ + + + + <% @vote.candidates.each do |candidate| %> + + <% end %> + + <% @vote.ratings.collect { |rating| rating.user }.uniq.each do |user| %> + + + <% @vote.candidates.each do |candidate| %> + <% if rating = @vote.ratings.find { |rating| rating.user == user and rating.candidate == candidate } %> + + <% end %> + <% end %> + + <% end %> +
Participant<%= candidate.name %>
<%= user.email %><%= rating.value %>
+

New candidate

diff --git a/vote.rb b/vote.rb index c6aeee9..bd35f3c 100644 --- a/vote.rb +++ b/vote.rb @@ -7,12 +7,13 @@ require 'securerandom' #set :database, 'sqlite3:db/vote.sqlite3' class Vote < ActiveRecord::Base - self.primary_key = "secure_id" has_many :candidates + has_many :ratings end class Candidate < ActiveRecord::Base belongs_to :vote + has_many :ratings end class User < ActiveRecord::Base @@ -20,6 +21,7 @@ class User < ActiveRecord::Base end class Rating < ActiveRecord::Base + belongs_to :vote belongs_to :user belongs_to :candidate end @@ -77,11 +79,7 @@ end get '/votes/:id' do redirect '/login' unless current_user - @vote = Vote.find(params[:id]) - @ratings = {} - @vote.candidates.each do |candidate| - @ratings[candidate] = Rating.where("user_id = ? and candidate_id = ?", current_user.id, candidate.id)[0] - end + @vote = Vote.find_by(secure_id: params[:id]) erb :votes_show end @@ -93,7 +91,7 @@ post '/votes' do end post '/votes/:id/candidates' do - @vote = Vote.find(params[:id]) + @vote = Vote.find_by(secure_id: params[:id]) @candidate = Candidate.new(name: params[:name], description: params[:description]) @candidate.vote = @vote @@ -103,11 +101,9 @@ end post '/votes/:id/ratings' do redirect '/login' unless current_user - vote = Vote.find(params[:id]) + vote = Vote.find_by(secure_id: params[:id]) vote.candidates.each do |candidate| - rating = Rating.find_by(user: current_user) - rating = Rating.find_by(candidate: candidate) - rating = Rating.find_or_initialize_by(user: current_user, candidate: candidate) + rating = Rating.find_or_initialize_by(vote: vote, user: current_user, candidate: candidate) rating.value = params[candidate.id.to_s] rating.save end