Forum: Ruby on Rails "validates_presence_of" not working

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms. (Guest)
on 2007-05-11 08:18
(Received via mailing list)
Hi, guys,

I'm learning to use Rails, and, while reading the "Rails: Up And
Running" book, I tried to make an insertion validation, using the
following code:

class Photo < ActiveRecord::Base
  validates_presence_of :filename

It simply didn't work, the validation does nothing and an insertion
with an empty field returns TRUE.

What's wrong?
George (Guest)
on 2007-05-11 08:40
(Received via mailing list)
On 5/11/07, <removed_email_address@domain.invalid> wrote:
> It simply didn't work, the validation does nothing and an insertion
> with an empty field returns TRUE.
> What's wrong?


Uh, well, it works for me.  You'll need to provide more information
for anyone to be able to help you I think.  In particular, exactly
what's returning "TRUE", and what you have in your record at the time.


$ cat db/migrate/001_create_initial_tables.rb
class CreateInitialTables < ActiveRecord::Migration
  def self.up
    create_table :photos do |t|
      t.column :filename, :string

  def self.down
    drop_table :photos

$ cat app/models/photo.rb
class Photo < ActiveRecord::Base
  validates_presence_of :filename

$ script/console
Loading development environment.
=> false
>> => '').valid?
=> false
>> => 'blah').valid?
=> true (Guest)
on 2007-05-11 21:07
(Received via mailing list)
Oops, sorry George, indeed I should have put some more information
about what's happening and what's the scene here...

There's a table in the db called "photos", with two columns, "id" and
"filename". "filename" is set for "NULL" as default, but I want to use
"validates_presence_of" as a mean to verify the field entry on the
console ("script/console").

When I do a "", and a "", in the console, it
should return "false", and generate the error strings for future
analisys, but it's returning "true" and it creates a new record in my
db with the "filename" field empty.

This was not supposed to happen because of
"validate_presence_of :filename", which I wrote in the "photo.rb"
model file.

Do I need to put more information? Thanks a lot, George!

Diogo (Guest)
on 2007-05-11 21:20
(Received via mailing list)
Hi, George,

Can't understand. Yesterday it was returning "true" in the console,
with empty field, and today, after I answered your message for the
first time, I tested it again, and it's returning "false"!!!

I tried to restart the server, and nothing, and, just now that I
restarted my computer, it's working! What's happening? Any clue?

Thanks, George, again!
Conrad T. (Guest)
on 2007-05-11 22:01
(Received via mailing list)
Hi, where's the code where you're trying to save the instance to the
database?  This should be somewhere in your controller code.  Also, can
provide your script/console transcript similar to what George provided?


This topic is locked and can not be replied to.