Created
November 28, 2016 16:11
-
-
Save universal/fe718ea35f43627242b755c2f3e84e74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book < ApplicationRecord | |
has_and_belongs_to_many :categories | |
validates :categories, presence: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Category < ApplicationRecord | |
has_and_belongs_to_many :books | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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