rails generate model User username email role critics_count:integer
rails g model Critic title body:text user:references
rails g model Company name description:text start_date:date country cover
rails g model Game name summary:text release_date:date category:integer rating:decimal cover
rails g model Platform name category:integer
rails g model Genre name
rails generate model InvolvedCompany company:references game:references developer:boolean publisher:booleanrails g migration CreateJoinTableGamePlatform game platform
rails g migration CreateJoinTableGameGenre game genreA Critic belongs to a user
belongs_to :userA User has many critics
has_many :critics, dependent: :destroyA Company has many involved_companies and has many games through involved_companies
class Company
has_many :involved_companies, dependent: :destroy
has_many :games, through: :involved_companies
endAn InvolvedCompany belongs to a company and belongs to a game
belongs_to :company
belongs_to :gameA Game has many involved_companies and has many companies through involved_companies
class Game
has_many :involved_companies, dependent: :destroy
has_many :companies, through: :involved_companies
endA Game has and belongs to many platforms
class Game
has_and_belongs_to_many :platforms
endA Game has and belongs to many genres
class Game
has_and_belongs_to_many :genres
endA Platform has and belongs to many games
class Platform
has_and_belongs_to_many :games
endA Genre has and belongs to many games
class Genre
has_and_belongs_to_many :games
enduser = User.create(username: "Teddy")
user.critics.create(title: "New title", body: "New Body")
user.critics.create(title: "New title 2", body: "New Body 2")
user.criticstestino = User.create(username: "testino")
testino.critics.create(title: 'New critic', body: 'New body')
testino = testino.critics
critic = testino.first
critic.usergame = Game.create(name: "The last of us")
company = Company.create(name: "Nautghy Doog")
InvolvedCompany.create(developer: true, publisher: false, game: game, company: company)
action_genre = Genre.create(name: 'action')
game.genres.push(action_genre)
platform_playstation = Platform.create(name: 'PlayStation 4', category: 1)
buscaminas.platforms << platform_playstationrails g migration AddParentRefToGame parent:referencesdef change
add_reference :games, :parent, foreign_key: { to_table: :games }
endclass Game
has_many :expansions, class_name: "Game",
foreign_key: "parent_id",
dependent: :destroy,
inverse_of: "parent"
belongs_to :parent, class_name: "Game", optional: true
endrails g migration AddCriticableToCritics criticable:references{polymorphic}class Critic
...
belongs_to :criticable, polymorphic: true
end
class Game
...
has_many :critics, as: :criticable, dependent: :destroy
end
class Company
...
has_many :critics, as: :criticable, dependent: :destroy
endrails g migration AddDefaultCategoryToGamesclass AddDefaultCategoryToGames < ActiveRecord::Migration[7.0]
def change
change_column_default(:games, :category, from:nil , to: 0)
end
endclass Game
enum category: { main_game: 0, expansion: 1 }
endrails g migration AddDefaultCategoryToPlatformclass AddDefaultCategoryToPlatform < ActiveRecord::Migration[7.0]
def change
change_column_default(:platforms, :category, from:nil , to: 0)
end
endclass Platform
...
enum category: {
console: 0,
arcade: 1,
platform: 2,
operating_system: 3,
portable_console: 4,
computer: 5
}
endTenemos que adicionar un valor por defecto a critics_count, mediante una migración:
rails g migration AddDefaultCountToUserclass AddDefaultCountToUser < ActiveRecord::Migration[7.0]
def change
change_column_default(:users, :critics_count, from:nil , to: 0)
end
endEn el modelo Critic adicionamos nuestros callbacks
class Critic
...
after_create :increment_critics_count
after_destroy :decrement_critics_count
private
def increment_critics_count
critic_user = user
critic_user.critics_count += 1
critic_user.save
end
def decrement_critics_count
critic_user = user
critic_user.critics_count -= 1
critic_user.save
end
endEn el modelo Critic hicimos el llamado a counter_cache
belongs_to :user, counter_cache: trueMigraciones para poder añadir indices a valores unicos
rails g migration AddIndexColumnNameToModel column:type:uniq
rails g migration AddIndexNameToPlatform name:string:uniq
rails g migration AddIndexNameToGenre name:string:uniq
rails g migration AddIndexNameToCompany name:string:uniq
rails g migration AddUniquennesCompanyIdGameIdToInvolvedCompany
Gran aporte Teddy!!!