Luke Redpath
2006-Feb-10 15:58 UTC
[Rails] Bizarre ActiveRecord::Errors/validation problem
I could be going mad and missing something really obvious here but I''m
getting an unexpected result from the following.
The schema:
create_table :services, :force => true do |t|
t.column :id, :primary_key
t.column :name, :string, :ilmit => 5, :null => false
t.column :port, :integer, :null => false
t.column :status, :string
end
The model:
class Service < ActiveRecord::Base
validates_presence_of :name, :port, :status
end
The unit tests:
class ServiceTest < Test::Unit::TestCase
def test_new_service
@service = Service.new
@service.name = ''www''
@service.port = 80
@service.status = ''Unknown''
assert @service.save
end
def test_invalid_service
@service = Service.new
assert !@service.save
assert_equal "can''t be blank",
@service.errors[''name'']
assert_equal "can''t be blank",
@service.errors[''status'']
assert_equal "can''t be blank",
@service.errors[''port'']
end
end
The first test passes fine, but the second test is failing on that last
assertion with:
=> <"can''t be blank"> expected but was
=> <nil>.
Any idea why the validate_presence_of method isn''t picking up the :port
column and providing an appropriate error message like it is with
name/status? Its not a reserved word or something is it?
To aid debugging, here is the output of:
@service.errors.each_full { |m| puts m }
=> Name can''t be blank
=> Status can''t be blank
As you can see, no sign of my error message for the port column.
Cheers
Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060210/769c7c25/attachment.html
Wilson Bilkovich
2006-Feb-10 20:59 UTC
[Rails] Bizarre ActiveRecord::Errors/validation problem
Did you copy and paste this? If so, you''ve got a typo here: t.column :name, :string, :ilmit => 5, :null => false "ilmit" instead of ''limit'' On 2/10/06, Luke Redpath <contact@lukeredpath.co.uk> wrote:> I could be going mad and missing something really obvious here but I''m > getting an unexpected result from the following. > > The schema: > > create_table :services, :force => true do |t| > t.column :id, :primary_key > t.column :name, :string, :ilmit => 5, :null => false > t.column :port, :integer, :null => false > t.column :status, :string > end > > > The model: > > class Service < ActiveRecord::Base > validates_presence_of :name, :port, :status > end > > The unit tests: > > class ServiceTest < Test::Unit::TestCase > def test_new_service > @service = Service.new > @ service.name = ''www'' > @service.port = 80 > @service.status = ''Unknown'' > assert @service.save > end > > def test_invalid_service > @service = Service.new > assert !@ service.save > assert_equal "can''t be blank", @service.errors[''name''] > assert_equal "can''t be blank", @service.errors[''status''] > assert_equal "can''t be blank", @service.errors [''port''] > end > end > > The first test passes fine, but the second test is failing on that last > assertion with: > > => <"can''t be blank"> expected but was > => <nil>. > > Any idea why the validate_presence_of method isn''t picking up the :port > column and providing an appropriate error message like it is with > name/status? Its not a reserved word or something is it? > > To aid debugging, here is the output of: > > @service.errors.each_full { |m| puts m } > => Name can''t be blank > => Status can''t be blank > > As you can see, no sign of my error message for the port column. > > Cheers > Luke > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >