Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dalvarez2596/many-to-many
Browse files Browse the repository at this point in the history
  • Loading branch information
dalvarez2596 committed Sep 7, 2024
2 parents d423d49 + 770f49b commit 7746395
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 104 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This repository is a Ruby on Rails application demonstrating the creation of a many-to-many relationship between `Player` and `Hero` models. Example data is generated using the Faker gem.

![erd](https://github.com/user-attachments/assets/2febd808-7675-4779-81d5-c4038661814c)

## Features

- **Faker Gem**: Used to generate fake data for development and testing purposes.
Expand All @@ -20,8 +22,8 @@ This repository is a Ruby on Rails application demonstrating the creation of a m
1. **Clone the repository**

```sh
git clone https://github.com/yourusername/your-repository-name.git
cd your-repository-name
git clone git@github.com:dalvarez2596/many-to-many.git
cd many-to-many
```

2. **Install dependencies**
Expand Down Expand Up @@ -91,3 +93,7 @@ This repository is a Ruby on Rails application demonstrating the creation of a m

- Ruby: 3.2.2
- Rails: 7.2.1

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
2 changes: 1 addition & 1 deletion app/controllers/heros_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def set_hero
end

def hero_params
params.require(:hero).permit(:name, :quote)
params.require(:hero).permit(:name, :quote, player_ids: [])
end
end
42 changes: 0 additions & 42 deletions app/controllers/player_heros_controller.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/models/hero.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class Hero < ApplicationRecord
has_many :player_heros
has_many :players, through: :player_heros
has_and_belongs_to_many :players
end
2 changes: 1 addition & 1 deletion app/models/player_hero.rb → app/models/heros_player.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PlayerHero < ApplicationRecord
class HerosPlayer < ApplicationRecord
belongs_to :player
belongs_to :hero
end
6 changes: 1 addition & 5 deletions app/models/player.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
class Player < ApplicationRecord
has_many :player_heros
has_many :heros, through: :player_heros

validates :name, presence: true
validates :team, presence: true
has_and_belongs_to_many :heros
end
4 changes: 4 additions & 0 deletions app/views/heros/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<div><%= message %></div>
<% end %>
</div>
<div>
<%= form.label :player_ids, 'Add players', style: 'display: block' %>
<%= form.select :player_ids, options_for_select(Player.all.pluck(:name, :id), hero.players.pluck(:id)), {include_hidden: false}, multiple: true %>
</div>
<div>
<%= form.submit %>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/views/heros/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<strong>Quote:</strong>
<%= @hero.quote %>
</p>
<p>
<strong>Players:</strong> <br>
<% @hero.players.each do |player| %>
<%= link_to "#{player.name}, #{player.team}", player_path(player) %> <br>
<% end %>
</p>
<%= link_to 'Edit Hero', edit_hero_path(@hero) %> |
<%= link_to 'Back to Heroes', heros_path %> |
<%= link_to 'Delete Hero', hero_path(@hero), data: {
Expand Down
16 changes: 1 addition & 15 deletions app/views/players/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,4 @@
<%= link_to 'Delete Player', player_path(@player), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
}%>
<h2>Add Hero to Player</h2>
<%= form_with url: player_heros_path(@player), method: :post do |f| %>
<%= f.collection_select :hero_id, Hero.all, :id, :name, prompt: "Select a hero" %>
<%= f.submit "Add Hero" %>
<% end %>
<h2>Heros Associated with <%= @player.name %></h2>
<ul>
<% @player.heros.each do |hero| %>
<li>
<%= hero.name %>
<%= button_to "Remove Hero", player_hero_path(@player, hero), method: :delete, data: { confirm: "Are you sure?" } %>
</li>
<% end %>
</ul>
}%>
5 changes: 1 addition & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
# Defines the root path route ("/")
# root "posts#index"
root "players#index"
resources :players do
resources :heros, only: [ :index, :create, :destroy ], controller: "player_heros"
end

resources :players
resources :heros
end
Binary file added db/.DS_Store
Binary file not shown.
Binary file added db/migrate/.DS_Store
Binary file not shown.
9 changes: 0 additions & 9 deletions db/migrate/20240906043436_create_join_table_player_hero.rb

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions db/migrate/20240907014512_create_join_table_player_hero.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateJoinTablePlayerHero < ActiveRecord::Migration[7.2]
def change
create_join_table :players, :heros do |t|
t.index [ :player_id, :hero_id ]
t.index [ :hero_id, :player_id ]
end
end
end
14 changes: 6 additions & 8 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Example:
#
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end

puts "Creating the array and filling with players and teams"
players = []
100.times do
Expand All @@ -30,3 +20,10 @@
puts "Filling the db with insert_all"
Hero.insert_all(heros)
puts "Data successfully loaded"

players_db = Player.all
heroes_db = Hero.all

35.times do
HerosPlayer.create(player: players_db.sample, hero: heroes_db.sample)
end

0 comments on commit 7746395

Please sign in to comment.