byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
439 stars 57 forks source link

write_attribute with symbol attr_name causes: undefined method `cast_type' for nil:NilClass #26

Closed dannytip closed 9 years ago

dannytip commented 9 years ago

I stumbled into this when using this with the amoeba gem.

activerecord-typedstore (0.5.0) lib/active_record/typed_store/extension.rb:132:in `coder_for'
activerecord-typedstore (0.5.0) lib/active_record/typed_store/extension.rb:142:in `write_attribute'
activerecord (4.2.0) lib/active_record/attribute_methods.rb:373:in `[]='
def coder_for(attr_name)
    column = self.class.columns_hash[attr_name]
    return unless column.cast_type.is_a?(::ActiveRecord::Type::Serialized)
    column.cast_type.coder
end

It seems it calls write_attribute() where "attr_name" is a symbol rather than a string.

Digging into active record it calls to_s on attr_name here: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_methods/write.rb#L59

Changing:

column = self.class.columns_hash[attr_name]

to

column = self.class.columns_hash[attr_name.to_s]

Should resolve the issue.