Question

Note: This situation is only happening in my rspec spec.

My understanding is that model.read_attribute(:attr_name) returns a typecasted attribute value. Which means if attr_name belongs to a integer column, read_attribute(:attr_name) will return me a Fixnum.

But...

model.read_attribute(:attr_name) returns a nil

model.attr_name returns a non-nil Fixnum value

Rails version: 4.0.3

Was it helpful?

Solution

TLDR version

My code expected the default value of attr_name to be at least 0, but due to certain circumstances, the default value of the attr_name column in the database table was null. Which resulted in the error I mentioned above.

Detailed explaination

The schema of my development database was out of sync with my production database (due to a few migrations that did not went into production). So whenever I ran a rake db:migrate, it created a schema.rb based on out-of-sync development database's schema. And this schema was then used by rake db:test:prepare, creating a database based on the outdated schema. Most importantly, the code had expected the value of attr_name to be at least 0 value from attr_name, but in the outdated schema, the attr_name column was null by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top