diff --git a/db/migrate/20250321235135_create_ratings.rb b/db/migrate/20250321235135_create_ratings.rb new file mode 100644 index 0000000..081a6f1 --- /dev/null +++ b/db/migrate/20250321235135_create_ratings.rb @@ -0,0 +1,10 @@ +class CreateRatings < ActiveRecord::Migration[7.2] + def change + create_table :ratings do |t| + t.belongs_to :user + t.belongs_to :candidate + t.integer :value + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c61f9f7..fd4484e 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_234644) do +ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do create_table "candidates", force: :cascade do |t| t.integer "vote_id" t.string "name" @@ -20,6 +20,16 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_234644) do t.index ["vote_id"], name: "index_candidates_on_vote_id" end + create_table "ratings", force: :cascade do |t| + t.integer "user_id" + t.integer "candidate_id" + t.integer "value" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["candidate_id"], name: "index_ratings_on_candidate_id" + t.index ["user_id"], name: "index_ratings_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email" t.string "password" diff --git a/views/votes_show.erb b/views/votes_show.erb index 3d3263f..052e20f 100644 --- a/views/votes_show.erb +++ b/views/votes_show.erb @@ -4,16 +4,28 @@

Hello, <%= current_user.email %>.

-

Candidates

+

Your ratings

+
+ +

New candidate

diff --git a/vote.rb b/vote.rb index b67954f..c6aeee9 100644 --- a/vote.rb +++ b/vote.rb @@ -16,6 +16,12 @@ class Candidate < ActiveRecord::Base end class User < ActiveRecord::Base + has_many :ratings +end + +class Rating < ActiveRecord::Base + belongs_to :user + belongs_to :candidate end def hash_password(password) @@ -72,6 +78,10 @@ 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 erb :votes_show end