Does find return true or false?

If I do a find such as
order = Order.find_by_name("John")
will 'order' be set to false ?
I want to make a condition where if no record is found , something happens.
So far no luck with the above though , thought I could confirm


Hi Stuart,

Dark Ambient wrote:

If I do a find such as
order = Order.find_by_name("John")
will 'order' be set to false ?

I believe you'll find that what 'find' returns depends on what you ask it for. I think if you ask it to 'find first' it returns either a single record or nil, and if you ask it to 'find all' it returns a (possibly empty) array.


Find will return nil when no record is found - so order will effectively be false.

This should work for you:

order = Order.find_by_name(“John”)
unless order

handle record not found


Here's my problem now :slight_smile: I am attempting to check for records before
allowing user to do a create. The problem is in the first line. should be available because I can get it to print out
in various views and it's in the application.rb controller. However
it doesn't seem to take. To test my method , if i say user_id = 10,
then things work.
in the Cdetail model user_id is a method (column). So basically what
I'm trying to say here is count how many records user_id with the
value of has.
i.e. right now i'm the only one on the system, so my
is 10. Somehow this translation is not taking place ?

def check_forec
      @result = Cdetail.count "user_id ="
      if @result >= 1[:warning] = 'Record Alread exists'
      redirect_to(:controller => 'index', :action => 'index')


The string “user_id =” is being passed straight into SQL, where
it probably just gives an error. You want “user_id = #{}”, which
will insert the value of into the string.

However, what you are doing seems like a major re-invention of the wheel -
you’re doing all the work for a has_one association. You probably want your

models to look like this:

class Cdetail < ActiveRecord::Base
belongs_to :user

class User < ActiveRecord::Base
has_one :cdetail, :dependent => :destroy

The “:dependent => :destroy” part will ensure that Cdetail records are not

left hanging around without associated User objects.

With that set up, you can access the associated Cdetail record like this:


and figure out if one exists with


Hope this helps,

Matt - Thank you so much and btw, apologies for the cross post. I
think I could use some remedial Ruby and go back to the books. I
completely forgot the #{ }. I did have the associations set up but no
the destory but it makes sense.


Take a look at the "validates_unique" validator, in addition to what
Matt Jones has already said.

I'm having a hard time using 'current_user.cdetail.nil?' in a before
filter. Maybe it's the way I'm writing it out. However the way things
are set up cdetails/new will create a new record. I wanted the filter
in the event that one existed. I guess it could be changed to ask if
it's 'not nil'.


I thought about the validates_unique, however this is a situation
where the user would create one record and then have the option later
of edit and destroy. I wouldn't want the user to enter in the
information again only to have the form bounce back.


That's a pretty common pattern - you can bounce it back with an error
message, WITH all his information intact to retry or do whatever.