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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2009-Mar-17 09:08 UTC
Re: Preventing a submitted hash from ActiveRecord DB store
validates_length_of :description, :minimum => 1 On Mar 17, 6:58 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > 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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-17 09:22 UTC
Re: Preventing a submitted hash from ActiveRecord DB store
On Mar 17, 5:58 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> 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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
@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!! :D <bangin head on wall> Thanks guys. returning "false" did the trick. On Mar 17, 2:22 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 17, 5:58 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > > 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?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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..
On Mar 17, 2:43 pm, Ram
<yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> @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!! :D <bangin head on wall>
>
> Thanks guys. returning "false" did the trick.
>
> On Mar 17, 2:22 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
> > On Mar 17, 5:58 am, Ram
<yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
>
> > > 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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- Setting DEFAULT_FIELD_OPTIONS in Rails 2.3
- mysql gem install not happening on Mac OS Leopard - any inputs at all??
- Observing few fields in a form - Best Practise?
- Read error: #<TypeError: can't modify frozen string> raised from HttpParser
- calling a jQuery function from RJS/onclick