开发者

Rails validation fails even when data is entered

Here is the command that I'm executing in Rails Console:

Person.create!(:firstName=>"matt", :lastName=>"master", :gender => 1)

My result is this error message:

Ac开发者_运维问答tiveRecord::RecordInvalid: Validation failed: Firstname can't be blank

My model validation code looks as such:

class Person < ActiveRecord::Base
belongs_to :user, :class_name => 'User', :foreign_key => 'fk_ssmUserId'
validates_presence_of :firstName, :lastName

When I comment out validates_presence_of everything works and my data is entered properly into the database, so I know that the values are actually being passed into the new object. I even inspected the new object created inside the Rails::ActiveRecord::Validations code to make sure it was being instantiated correctly before being saved. It was. Also, I have other models with validates_presence_of that work 100% fine every time. It's just this one model. I am using Rails 3.1.0.rc1.

Any ideas?

Thanks!


:firstName=>"matt", :lastName=>"master", :gender => 1

check that the key correspond to your model columns. Seems to be everything should be fine.


I have a suspicion that this error relates to the fact that you're creating and saving the object using the .create! method from the console. Your Person class appears to require a foreign key, a value which is probably not being instantiated when you create an object at the console. To test this, try typing:

test = Person.create(:firstName=>"matt", :lastName=>"master", :gender => 1)

with no bang after the .create method. This should not generate an error. Now type:

test

It's very likely that you have required key values set as "nil" and that the object can't be saved from console until you fill in the appropriate values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜