# Preventing a submitted hash from ActiveRecord DB store

**URL:** <https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079>\
**Category:** Rails\
**Created:** [March 17, 2009, 6:59am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079 "2009-03-17T06:59:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [March 17, 2009, 6:59am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079/1 "2009-03-17T06:59:16Z")

</div>

Hi all,

I have a multi model form (Project with many tasks) and I want to  
prevent a task from being saved to the DB if it is empty ie. if there  
is no i/p for that task from the user. I tried the following

class Task \< ActiveRecord::Base

before\_save :check\_if\_empty  
…

def check\_if\_empty  
self.destroy if description.blank?  
end

but i get this

TypeError in ProjectsController#create  
can’t modify frozen hash

Is there a way to let ActiveRecord know not to save a record (which is  
a frozen hash in this case) to the DB?

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [March 17, 2009, 10:08am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079/2 "2009-03-17T10:08:48Z")

</div>

validates\_length\_of :description, :minimum =\> 1

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [March 17, 2009, 10:23am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079/3 "2009-03-17T10:23:29Z")

</div>

On Mar 17, 5:58 am, Ram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I have a multi model form (Project with many tasks) and I want to  
> prevent a task from being saved to the DB if it is empty ie. if there  
> is no i/p for that task from the user. I tried the following

If you don’t want the save to happen then you should return false from  
your before\_save.

Fred

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [March 17, 2009, 10:44am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079/4 "2009-03-17T10:44:36Z")

</div>

@Wolas!,

I figured a validation will do the trick but knew something simpler  
had to be there.

@Fred,

Dint count on it being SO simple!! 😃

Thanks guys. returning “false” did the trick.

On Mar 17, 2:22 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 3, 2009, 7:42am UTC](https://www.ruby-forum.com/t/preventing-a-submitted-hash-from-activerecord-db-store/163079/5 "2009-04-03T07:42:20Z")

</div>

Hi Fred,

I had Project has\_many Tasks and i had the following before\_save  
callback on Task

def capitalis  
if (name.blank?)  
false  
else  
self.name = name.capitalize  
end  
end

Now I need to make Project and Task share a HABTM relationship. I  
started experimenting with it after establishing the associations.  
Created a new Project, with some tasks in it and left on task blank. I  
got an ActiveRecord::RecordNotSaved error for the blank Task.  
Now I understand it is because of the false return in the callback.  
And this also cancels all following callbacks and association  
callbacks (?). So although the valid Tasks and the Project itself DO  
get saved to the DB, I get this error.  
Is it neat enough to rescue from this specific error in the controller  
and ignore it? Seems slightly hacky… so, is there a nicer way to  
handle this?  
Thanks…
