Skip to content

Instantly share code, notes, and snippets.

@gustavowt
Created May 27, 2022 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavowt/599f22715e9f8e27935cb719f31fddde to your computer and use it in GitHub Desktop.
Save gustavowt/599f22715e9f8e27935cb719f31fddde to your computer and use it in GitHub Desktop.
class Animal < ApplicationRecord
belongs_to :place, polymorphic: true
end
class Monkey < Animal
validates :name, presence: true
end
class Cat < Animal
validates :breed, presence: true
end
class Jungle < ApplicationRecord
has_many :animals, as: :place
accepts_nested_attributes_of :animals,
end
=> jungle = Jungle.new(animals_attributes: [{ type: 'Monkey', name: nil}, { type: 'Cat', beed: nil }])
=> jungle.valid?
=> false
=> jungle.errors.full_messages
=> ["Animals name can't be blank", "Animals breed can't be blank"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment