added validation for models

This commit is contained in:
Aaro Koivukangas
2023-08-10 18:09:01 +03:00
parent b0ab392fc2
commit 8fb5db0025
4 changed files with 14 additions and 0 deletions
+2
View File
@@ -4,6 +4,8 @@ class Beer < ApplicationRecord
belongs_to :brewery belongs_to :brewery
has_many :ratings, dependent: :destroy has_many :ratings, dependent: :destroy
validates :name, presence: true
def to_s def to_s
"#{name}, #{(Brewery.find_by_id brewery_id).name}" "#{name}, #{(Brewery.find_by_id brewery_id).name}"
end end
+5
View File
@@ -4,6 +4,11 @@ class Brewery < ApplicationRecord
has_many :beers, dependent: :destroy has_many :beers, dependent: :destroy
has_many :ratings, through: :beers has_many :ratings, through: :beers
validates :name, presence: true
validates :year, numericality: { greater_than_or_equal_to: 1040,
less_than_or_equal_to: 2023,
only_integer: true
}
def print_report def print_report
puts name puts name
puts "established at year #{year}" puts "established at year #{year}"
+5
View File
@@ -2,6 +2,11 @@ class Rating < ApplicationRecord
belongs_to :beer belongs_to :beer
belongs_to :user belongs_to :user
validates :score, numericality: { greater_than_or_equal_to: 1,
less_than_or_equal_to: 50,
only_integer: true
}
def to_s def to_s
"#{beer.name}: #{score}" "#{beer.name}: #{score}"
end end
+2
View File
@@ -1,5 +1,7 @@
class User < ApplicationRecord class User < ApplicationRecord
include RatingAverage include RatingAverage
validates :username, uniqueness: true, length: { minimum: 3, maximum: 30 }
has_many :ratings has_many :ratings
end end