Skip to content

Instantly share code, notes, and snippets.

@clarkngo
Created October 3, 2018 03:40
Show Gist options
  • Save clarkngo/f541545896e9d83d478ce312cd24ed84 to your computer and use it in GitHub Desktop.
Save clarkngo/f541545896e9d83d478ce312cd24ed84 to your computer and use it in GitHub Desktop.
class Book < ApplicationRecord
validates :title, presence: true
validates :author, presence: true
validates :genre, presence: true
validates :classification, presence: true, :inclusion { :in => @allowed_classifications }
@allowed_classifications = [
'General Works - encyclopedias',
'Philosophy, Psychology, Religion',
'History - Auxiliary Sciences',
'History (except American)',
'General U.S. History',
'Local U.S. History',
'Geography, Anthropology, Recreation',
'Social Sciences U',
'Political Science V',
'Law Z - Bibliography and Library Science',
'Education',
'Music',
'Fine Arts',
'Language and Literature',
'Science',
'Medicine',
'Agriculture',
'Technology',
'Military',
'Naval Science',
'Bibliography and Library Science',
'type'
]
end
@conradwt
Copy link

conradwt commented Oct 3, 2018

I recommend using a class method called classifications instead of an instance variable here which returns an array. Also, you're missing the book_type and year validations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment