ruby on rails - Rais - How to prevent deleting all children records of a parent -



ruby on rails - Rais - How to prevent deleting all children records of a parent -

listing < ar has_many :images accepts_nested_attributes_for :images, :allow_destroy => true validate :validate_image_count def validate_image_count errors.add_to_base("too few") if images.length < 1 end end image < ar belongs_to :listing end

in listing#edit form utilize fields_for provide fields images along checkboxes delete images. working fine. want enforce check such listing valid if had @ to the lowest degree 1 image , @ 6.

in current setup can go edit , delete images, , update listing.

i have tried using validation shown above thats not beingness called. way nested_attributes work in rails. whats best way enforce check?

as images won't deleted when phone call validation method homecoming true on image length. can utilize marked_for_destruction?

def validate_image_count errors.add_to_base("too few") self.images.any? { |i| i.marked_for_destruction? } end

ruby-on-rails validation nested-forms nested-attributes fields-for

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -