Hi I have done a drop down menu (collection_select) were I included a blank space. Then in my controller I want it to evaluate if the chosen drop down option is nil. I do this and keep getting errors: if @category.parent_id.nil? @category.parent_id = "0" end Is it a syntax problem or something else? JoeRails --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Tue, 16 Oct 2007, JoeRails wrote:> > Hi > > I have done a drop down menu (collection_select) were I included a > blank space. Then in my controller I want it to evaluate if the chosen > drop down option is nil. I do this and keep getting errors: > > if @category.parent_id.nil? > @category.parent_id = "0" > end > > > Is it a syntax problem or something else?It''s something else. What error message are you getting? David -- Upcoming training from Ruby Power and Light, LLC: * Intro to Ruby on Rails, Edison, NJ, October 23-26 * Advancing with Rails, Edison, NJ, November 6-9 Both taught by David A. Black. See http://www.rubypal.com for more info! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
i get the following error: Mysql::Error: Column ''parent_id'' cannot be null: INSERT INTO categories (`name`, `description`, `parent_id`, `created_at`) VALUES(''adfadfadfasdf'', ''adsfasdfasdfasdf'', NULL, ''2007-10-16 17:46:44'') thanks, jonathan On Oct 16, 5:42 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Tue, 16 Oct 2007, JoeRails wrote: > > > Hi > > > I have done a drop down menu (collection_select) were I included a > > blank space. Then in my controller I want it to evaluate if the chosen > > drop down option is nil. I do this and keep getting errors: > > > if @category.parent_id.nil? > > @category.parent_id = "0" > > end > > > Is it a syntax problem or something else? > > It''s something else. What error message are you getting? > > David > > -- > Upcoming training from Ruby Power and Light, LLC: > * Intro to Ruby on Rails, Edison, NJ, October 23-26 > * Advancing with Rails, Edison, NJ, November 6-9 > Both taught by David A. Black. > Seehttp://www.rubypal.comfor more info!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
JoeRails wrote:> i get the following error: > > Mysql::Error: Column ''parent_id'' cannot be null: INSERT INTO > categories (`name`, `description`, `parent_id`, `created_at`) > VALUES(''adfadfadfasdf'', ''adsfasdfasdfasdf'', NULL, ''2007-10-16 > 17:46:44'') > > thanks, > > jonathanI''m gonna take my best stab at this. the parent_id should always be an integer, so it says you are trying to update that column with nil. So if it is trying to update it with that blank space, you need to tell it that the blank space is equal to 0, or nil.to_i ~Jeremy -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On Tue, 16 Oct 2007, JoeRails wrote:> > > i get the following error: > > Mysql::Error: Column ''parent_id'' cannot be null: INSERT INTO > categories (`name`, `description`, `parent_id`, `created_at`) > VALUES(''adfadfadfasdf'', ''adsfasdfasdfasdf'', NULL, ''2007-10-16 > 17:46:44'')The error is that column ''parent_id'' cannot be null :-) It''s strictly a database thing. Your database has a constraint that this column can''t be null, so when you do: @category = Category.create(params[:category]) # or whatever it bombs if params[:category] is nil, because that gets inserted into the database as NULL. You have a couple of choices. If you want to (or need to) keep the constraint, then you either need to test for nil and take corrective action, or rescue from the Mysql::Error. Another approach is to allow the NULL in the database, but put an application-level validation in the model that ensures that every category has a parent. How much data-checking to do at the database level, and how much at the application level (i.e., before database assertion is even attempted), is very dependent on lots of factors, some of them organization (e.g., who has access to the database?). So you can''t just make the decision for this one example; it needs to be thought through carefully. But anyway, that''s the general area of the issue. David> thanks, > > jonathan > > On Oct 16, 5:42 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: >> Hi -- >> >> On Tue, 16 Oct 2007, JoeRails wrote: >> >>> Hi >> >>> I have done a drop down menu (collection_select) were I included a >>> blank space. Then in my controller I want it to evaluate if the chosen >>> drop down option is nil. I do this and keep getting errors: >> >>> if @category.parent_id.nil? >>> @category.parent_id = "0" >>> end >> >>> Is it a syntax problem or something else? >> >> It''s something else. What error message are you getting? >> >> David >> >> -- >> Upcoming training from Ruby Power and Light, LLC: >> * Intro to Ruby on Rails, Edison, NJ, October 23-26 >> * Advancing with Rails, Edison, NJ, November 6-9 >> Both taught by David A. Black. >> Seehttp://www.rubypal.comfor more info! > > > >-- Upcoming training from Ruby Power and Light, LLC: * Intro to Ruby on Rails, Edison, NJ, October 23-26 * Advancing with Rails, Edison, NJ, November 6-9 Both taught by David A. Black. See http://www.rubypal.com for more info! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I was actually doing the validation in the controller not the model, here is the complete method in the controller: def create @categories = Category.find(:all, :conditions => ["parent_id = ?", 0]) if request.post? @category = Category.create(params[:category]) if @category.parent_id.nil? @category.parent_id = "0" end if @category.save redirect_to :action => "categories" end end end All I tried to do was check if it was nil, assign a 0 so that it would be considered a parent_id=0 which would be a main category. What I don''t understand is why would it find an error if there are two options only. It either chooses a category from the drop down (which gives it a parent id of the main category) or the user leaves it blank, thus the evaluation would add a cero, signifying it is a parent.as you suggested the software side is much cleaner than the databse side. What could be wrong with the code? Why is it still sending a null if I am specifying to add a "0" if it is nil? (I really appreciate all you guy''s help...i am a newbie at programing) thanks, jonathan On Oct 16, 6:01 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Tue, 16 Oct 2007, JoeRails wrote: > > > i get the following error: > > > Mysql::Error: Column ''parent_id'' cannot be null: INSERT INTO > > categories (`name`, `description`, `parent_id`, `created_at`) > > VALUES(''adfadfadfasdf'', ''adsfasdfasdfasdf'', NULL, ''2007-10-16 > > 17:46:44'') > > The error is that column ''parent_id'' cannot be null :-) > > It''s strictly a database thing. Your database has a constraint that > this column can''t be null, so when you do: > > @category = Category.create(params[:category]) # or whatever > > it bombs if params[:category] is nil, because that gets inserted into > the database as NULL. > > You have a couple of choices. If you want to (or need to) keep the > constraint, then you either need to test for nil and take corrective > action, or rescue from the Mysql::Error. Another approach is to allow > the NULL in the database, but put an application-level validation in > the model that ensures that every category has a parent. > > How much data-checking to do at the database level, and how much at > the application level (i.e., before database assertion is even > attempted), is very dependent on lots of factors, some of them > organization (e.g., who has access to the database?). So you can''t > just make the decision for this one example; it needs to be thought > through carefully. But anyway, that''s the general area of the issue. > > David > > > > > thanks, > > > jonathan > > > On Oct 16, 5:42 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote: > >> Hi -- > > >> On Tue, 16 Oct 2007, JoeRails wrote: > > >>> Hi > > >>> I have done a drop down menu (collection_select) were I included a > >>> blank space. Then in my controller I want it to evaluate if the chosen > >>> drop down option is nil. I do this and keep getting errors: > > >>> if @category.parent_id.nil? > >>> @category.parent_id = "0" > >>> end > > >>> Is it a syntax problem or something else? > > >> It''s something else. What error message are you getting? > > >> David > > >> -- > >> Upcoming training from Ruby Power and Light, LLC: > >> * Intro to Ruby on Rails, Edison, NJ, October 23-26 > >> * Advancing with Rails, Edison, NJ, November 6-9 > >> Both taught by David A. Black. > >> Seehttp://www.rubypal.comformore info! > > -- > Upcoming training from Ruby Power and Light, LLC: > * Intro to Ruby on Rails, Edison, NJ, October 23-26 > * Advancing with Rails, Edison, NJ, November 6-9 > Both taught by David A. Black. > Seehttp://www.rubypal.comfor more info!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/16/07, JoeRails <jonathantarud-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I was actually doing the validation in the controller not the model, > here is the complete method in the controller: > > def create > @categories = Category.find(:all, :conditions => ["parent_id = ?", > 0]) > if request.post? > @category = Category.create(params[:category])Shouldn''t this be: @category = Category.new(params[:category]) you''re trying to save it before fixing it up. But this might best be accomplished in the model with something like a before_save callback -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
holy snikies...it worked!!!! i thought the errror was in the evaluation. and it was in the .create thanks a lot!!!! On Oct 17, 7:43 am, "Rick DeNatale" <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10/16/07, JoeRails <jonathanta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I was actually doing the validation in the controller not the model, > > here is the complete method in the controller: > > > def create > > @categories = Category.find(:all, :conditions => ["parent_id = ?", > > 0]) > > if request.post? > > @category = Category.create(params[:category]) > > Shouldn''t this be: > @category = Category.new(params[:category]) > > you''re trying to save it before fixing it up. > > But this might best be accomplished in the model with something like a > before_save callback > > -- > Rick DeNatale > > My blog on Rubyhttp://talklikeaduck.denhaven2.com/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/17/07, JoeRails <jonathantarud-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > holy snikies...it worked!!!! > > i thought the errror was in the evaluation. and it was in the .createThe stack trace will show the file and line number in your code where the error is coming from, so you need to learn how to read those (you may have to look through the trace until you get out of the rails code and into your application''s code). You probably would have realized your problem immediately, or at the least, been able to post the code including the offending line. It''s very common to assume that an error is coming from one part of the code when it''s actually coming from another. We all have to resist the temptation to make assumptions like that. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thanks for that tip....i recreated the bug and still get the mysql error, but just as you said, it shows me the error in the line where the create messed up in the Application trace. My question is: which one of the two errors should be taken care of first when developing, since the trace donse''t really say much...in this case it outputs this(last two lines): /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:451:in `create'' app/controllers/admin_controller.rb:16:in `create'' Does this mean it could only get till that line? if so, is the error always in the last line of the trace? thanks, jonathan On Oct 17, 10:05 am, "Bob Showalter" <showa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 10/17/07, JoeRails <jonathanta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > holy snikies...it worked!!!! > > > i thought the errror was in the evaluation. and it was in the .create > > The stack trace will show the file and line number in your code where > the error is coming from, so you need to learn how to read those (you > may have to look through the trace until you get out of the rails code > and into your application''s code). You probably would have realized > your problem immediately, or at the least, been able to post the code > including the offending line. > > It''s very common to assume that an error is coming from one part of > the code when it''s actually coming from another. We all have to resist > the temptation to make assumptions like that.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''ll let others talk on other matters, but parent_id is supposed to be an integer, and you are trying to make it a string. I don''t know, but I suspect strongly that this is a problem. Student On Oct 17, 10:36 am, JoeRails <jonathanta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> thanks for that tip....i recreated the bug and still get the mysql > error, but just as you said, it shows me the error in the line where > the create messed up in the Application trace. > > My question is: which one of the two errors should be taken care of > first when developing, since the trace donse''t really say much...in > this case it outputs this(last two lines): > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:451:in `create'' > app/controllers/admin_controller.rb:16:in `create'' > > Does this mean it could only get till that line? if so, is the error > always in the last line of the trace? > > thanks, > > jonathan > > On Oct 17, 10:05 am, "Bob Showalter" <showa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 10/17/07, JoeRails <jonathanta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > holy snikies...it worked!!!! > > > > i thought the errror was in the evaluation. and it was in the .create > > > The stack trace will show the file and line number in your code where > > the error is coming from, so you need to learn how to read those (you > > may have to look through the trace until you get out of the rails code > > and into your application''s code). You probably would have realized > > your problem immediately, or at the least, been able to post the code > > including the offending line. > > > It''s very common to assume that an error is coming from one part of > > the code when it''s actually coming from another. We all have to resist > > the temptation to make assumptions like that.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---