Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Many to Many Relationship #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gem "rake"

# Provides functionality to interact with a SQLite3 database
# https://github.com/sparklemotion/sqlite3-ruby
gem "sqlite3", "~> 1.4"
gem "sqlite3", "~> 1.7"

# Require all files in a folder
# https://github.com/jarmo/require_all
Expand All @@ -25,7 +25,7 @@ group :development do
# Used to generate seed data
# https://github.com/faker-ruby/faker
gem "faker", "~> 2.18"

gem "pry"
end

Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ GEM
sinatra-activerecord (2.0.23)
activerecord (>= 4.1)
sinatra (>= 1.0)
sqlite3 (1.4.2)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-darwin)
tilt (2.0.10)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
zeitwerk (2.4.2)

PLATFORMS
arm64-darwin-23
universal-darwin-20

DEPENDENCIES
Expand All @@ -77,7 +79,7 @@ DEPENDENCIES
require_all
rspec
sinatra-activerecord
sqlite3 (~> 1.4)
sqlite3 (~> 1.7)

BUNDLED WITH
2.2.23
1 change: 1 addition & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Game < ActiveRecord::Base
has_many :reviews
has_many :users, through: :reviews
end
1 change: 1 addition & 0 deletions app/models/review.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Review < ActiveRecord::Base
belongs_to :game
belongs_to :user
end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class User < ActiveRecord::Base

has_many :reviews
has_many :games, through: :reviews
end
9 changes: 9 additions & 0 deletions db/migrate/20240719174157_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end

5 changes: 5 additions & 0 deletions db/migrate/20240719174300_add_user_id_to_reviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUserIdToReviews < ActiveRecord::Migration[6.1]
def change
add_column :reviews, :user_id, :integer
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_07_19_113221) do
ActiveRecord::Schema.define(version: 2024_07_19_174300) do

create_table "games", force: :cascade do |t|
t.string "title"
Expand All @@ -27,6 +27,13 @@
t.integer "game_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "user_id"
end

create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

end