Created
February 4, 2021 00:10
-
-
Save georgekettle/22757ce6604b826b60e639821a735c35 to your computer and use it in GitHub Desktop.
ActiveRecord associations & Validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateTeams < ActiveRecord::Migration[6.0] | |
def change | |
create_table :teams do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreatePlayers < ActiveRecord::Migration[6.0] | |
def change | |
create_table :players do |t| | |
t.string :name | |
t.references :team, foreign_key: true | |
t.timestamps | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateUsers < ActiveRecord::Migration[6.0] | |
def change | |
create_table :users do |t| | |
t.string :email | |
t.timestamps | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateLikes < ActiveRecord::Migration[6.0] | |
def change | |
create_table :likes do |t| | |
t.references :player, foreign_key: true | |
t.references :user, foreign_key: true | |
t.timestamps | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddNameToUsers < ActiveRecord::Migration[6.0] | |
def change | |
add_column :users, :first, :string | |
add_column :users, :last, :string | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Like < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :player | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player < ActiveRecord::Base | |
belongs_to :team | |
has_many :likes | |
has_many :users, through: :likes | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Team < ActiveRecord::Base | |
has_many :players | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :likes | |
validates :first, :last, presence: true | |
validates :first, length: { minimum: 3 } | |
validates :email, uniqueness: true | |
validates :email, format: { with: /\A.*@.*\.com\z/ } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment