Advertisement
Guest User

book.rb

a guest
May 6th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. # == Schema Information
  2. #
  3. # Table name: books
  4. #
  5. # id :bigint not null, primary key
  6. # title :string
  7. # release_year :integer
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. #
  11. class Book < ApplicationRecord
  12. include Hashid::Rails
  13.  
  14. attr_accessor :book_authors_attributes
  15.  
  16. has_many :chapters, dependent: :destroy
  17. has_many :book_authors, dependent: :destroy
  18.  
  19. validates :title, presence: true
  20.  
  21. accepts_nested_attributes_for :book_authors, allow_destroy: true
  22.  
  23. # Outputs a formatted list of the book's authors
  24. def author_str
  25. authors = ""
  26. book_authors.each do |author|
  27. authors << "#{author.author_name}, "
  28. end
  29. authors.delete_suffix(', ')
  30. end
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement