Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning a model attribute as nil doesn't create an attribute #155

Open
mjobrien opened this issue Dec 17, 2018 · 1 comment
Open

Assigning a model attribute as nil doesn't create an attribute #155

mjobrien opened this issue Dec 17, 2018 · 1 comment
Milestone

Comments

@mjobrien
Copy link
Contributor

mjobrien commented Dec 17, 2018

The protected write_attribute method of SparkApi::Models::Base checks whether the value being set for an attribute is already set to the assigned value. This causes inconsistent behavior when the attribute is new and the assigned value is nil because attributes is a hash and returns nil when a key does not exist.

l = SparkApi::Models::Listing.new
l.attributes # {}
l.City       # NoMethodError: undefined method `include?' for nil:NilClass
l.State = 'ND'
l.City = nil
l.State      # "ND"
l.City       # NoMethodError: undefined method `include?' for nil:NilClass
l.attributes # {"State"=>"ND"}
l.attributes['City'] = nil
l.City       # nil
l.attributes # {"State"=>"ND", "City"=>nil}
l.changes    #{"State"=>[nil, "ND"]}

This can be partially worked around by writing to the attributes hash directly. However, as illustrated above, that isn't a full-featured approach with respect to the SparkApi::Models::Dirty module at least.

On second thought, l.changes isn't a great example of the workaround not being full-featured. However, the general point remains that this is inconsistent behavior.

@bhornseth
Copy link
Member

This seems like it'd be a good candidate for the 2.0 milestone, yeah?

@bhornseth bhornseth added this to the 2.0 milestone Dec 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants