Created
February 29, 2012 01:28
-
-
Save erkattak/1936811 to your computer and use it in GitHub Desktop.
validate presence of polymorphic object when accepting nested attributes?
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 Business < ActiveRecord::Base | |
has_one :location, as: :locatable | |
accepts_nested_attributes_for :location, allow_destroy: true, reject_if: -> (attributes) { attributes['address'].blank? } | |
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 Location < ActiveRecord::Base | |
belongs_to :locatable, polymorphic: true | |
validates_presence_of :locatable, unless: ->(location) { location.locatable_type.present? } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, inverse_of will not work for polymorphic associations - http://guides.rubyonrails.org/association_basics.html#bi-directional-associations