test/templates/app/models/city.rb in dry_crud-1.2.5 vs test/templates/app/models/city.rb in dry_crud-1.2.6
- old
+ new
@@ -1,13 +1,25 @@
class City < ActiveRecord::Base
has_many :people
- validates_presence_of :name, :country_code
+ validates :name, :presence => true
+ validates :country_code, :presence => true
+ before_destroy :protect_with_inhabitants
+
default_scope order('country_code, name')
def label
"#{name} (#{country_code})"
+ end
+
+ protected
+
+ def protect_with_inhabitants
+ if people.exists?
+ errors.add(:base, "You cannot destroy this city as long as it has any inhabitants")
+ false
+ end
end
end
\ No newline at end of file