spinlock
spinlock

Reputation: 3957

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.

Upvotes: 0

Views: 235

Answers (1)

HakonB
HakonB

Reputation: 7075

Your second example uses mass-assignment whereas the first uses direct assignment. Did you by any change prohibit mass-assignment to taken_at?

Upvotes: 1

Related Questions