Why aren't Rails model attributes accessible using symbols instead of strings?
The Hash returned when you call #attributes
on a AR instance has string keys, which is why a symbol as an index into the hash doesn't work in your case. There is a subclass of Hash
called HashWithIndifferentAccess
which automatically converts symbol indexes into strings.
Quite often in Rails you'll encounter HashWithIndifferentAccess
instances. A perfect example is the params variable you access in your controller and view code.
Try using work_effort.attributes.with_indifferent_access[key]
Really it is just doing the same thing that you are, but it does it behind the scenes.
You can overwrite the attributes method with your own.
Open your WorkEffort class
class WorkEffort
def attributes
super.symbolize_keys
end
end
then when you call work_effort.attributes, you will have symbolized keys in the hash.