create! not setting datetime attribute in rails3
I have a datatime attribute taken_at
in one of my models that will record when a test was taken. I'm trying to validate that this field is always present开发者_高级运维 in my model with the following line:
validates :taken_at, :presence => true
In my rspec tests, I'm validating this rule with the following test:
before (:each) do
@user = Factory(:user)
@attr = { :taken_at => DateTime.now }
end
it "should create a new profile given valid attributes" do
# set taken_at inside of a block because the validations fail otherwise.
@user.hartman_value_profiles.create! do |hvp|
hvp.taken_at = DateTime.now
end
end
As you can see, I'm setting taken_at
inside a block rather than passing it in as parameter. This is working but I don't understand why the following test will fail:
it "should create a new profile given valid attributes" do
@user.hartman_value_profiles.create!(@attr)
end
Does anyone have any insight into what is going on here? Any tips, tricks or pointers to the documentation would be greatly appreciated.
Your second example uses mass-assignment whereas the first uses direct assignment. Did you by any change prohibit mass-assignment to taken_at?
精彩评论