ActiveRecord has_many associations

Given the models Country, State, City and Person as follows.

class Country < ActiveRecord::Base

has_many :states

end

class State < ActiveRecord::Base

belongs_to :country

has_many :cities

end

class City < ActiveRecord::Base

belongs_to :state

has_many :people

end

class Person < ActiveRecord::Base

belongs_to :city

end

Is there any way that doesn’t allow to delete a country if it has states? (will do the same for states and cities, cities and persons). Didn’t saw nothing like that in the dependent option.

Thanks in advance.

Hi!

I’m new to Rails, but I’ll try to help.

Maybe you could use the before_destroy callback:

http://guides.rubyonrails.org/active_record_validations_callbacks.html#destroying-an-object

Best Regards,

Everaldo