mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
added validation for models
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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}"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user