Evaluating nil

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

Hi –

On Tue, 16 Oct 2007, JoeRails wrote:

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!

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

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,

jonathan

I’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

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 :slight_smile:

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

It’s something else. What error message are you getting?


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!

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 10/16/07, JoeRails [email protected] 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/

holy snikies…it worked!!!

i thought the errror was in the evaluation. and it was in the .create

thanks a lot!!!

On 10/17/07, JoeRails [email protected] 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.

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:increate’

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs