Skip to content

Instantly share code, notes, and snippets.

@BartlomiejSkwira
Created September 6, 2013 09:06
Show Gist options
  • Select an option

  • Save BartlomiejSkwira/6461368 to your computer and use it in GitHub Desktop.

Select an option

Save BartlomiejSkwira/6461368 to your computer and use it in GitHub Desktop.
accepts_nested_attributes
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