views:

35

answers:

1

This might sound odd, but is there a 'Rails way' to have a model destroyed if a certain attribute is blank? Say I have a model like tags with just a name attribute or something, if the user edits the tag and deletes all the text out of the name field in the form I'd like the model to just be deleted.

I'm aware of the reject_if method, but that doesn't seem to work.

+1  A: 

On the after_save callback, just check the attribute and destroy the model if it's blank. Something like:

def Tag < ActiveRecord::Base
  after_save { |tag| tag.destroy if tag.name.blank? }
end
Dave Sims
That works, just curious if there was some super epic railsy way to do with like a 3 letter method... yah know how they like to surprise ya sometimes...
Joseph Silvashy
lol, one line not good enough for ya? : ) I know what you mean though. Ruby does tend to spoil you.
Dave Sims
your solution is great, but ya I guess I always feel like I'm doing things wrong when I see little shortcuts etc, thanks again!
Joseph Silvashy