Created
September 6, 2013 09:06
-
-
Save BartlomiejSkwira/6461368 to your computer and use it in GitHub Desktop.
accepts_nested_attributes
This file contains hidden or 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 Availability < ActiveRecord::Base | |
| has_many :availability_periods, dependent: :destroy, inverse_of: :availability | |
| accepts_nested_attributes_for :availability_periods, allow_destroy: true | |
| validates_associated :availability_periods | |
| validate :must_have_periods | |
| def must_have_periods | |
| if availability_periods.empty? or availability_periods.all? {|period| period.marked_for_destruction? } | |
| errors.add(self.property.name, I18n.t('models.availability.periods_required')) | |
| end | |
| end | |
| end | |
| class AvailabilityPeriod < ActiveRecord::Base | |
| belongs_to :availability, inverse_of: :availability_periods | |
| validates :date_range, :availability, presence: true | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment