Skip to content

Instantly share code, notes, and snippets.

@universal
Created November 28, 2016 16:11
Show Gist options
  • Save universal/fe718ea35f43627242b755c2f3e84e74 to your computer and use it in GitHub Desktop.
Save universal/fe718ea35f43627242b755c2f3e84e74 to your computer and use it in GitHub Desktop.
class Book < ApplicationRecord
has_and_belongs_to_many :categories
validates :categories, presence: true
end
class Category < ApplicationRecord
has_and_belongs_to_many :books
end
c = Category.create title: "Some Category"
b = Book.new title: "Some Book"
unless b.valid?
puts b.errors.inspect
end
b.categories << c
if b.valid?
puts "book is now valid!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment