solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

attributes method doesn't behave as specified in the Readme #352

Closed alaibe closed 8 years ago

alaibe commented 8 years ago

Hi This is the readme:

class User
  include Virtus.model

  attribute :name, String
  attribute :age, Integer
  attribute :birthday, DateTime
end

user = User.new(:name => 'Piotr', :age => 31)
user.attributes # => { :name => "Piotr", :age => 31 }

This is from my irb

class User
  include Virtus.model

  attribute :name, String
  attribute :age, Integer
  attribute :birthday, DateTime
end

user = User.new(:name => 'Piotr', :age => 31)
user.attributes # => { :name => "Piotr", :age => 31, :birthday=>nil }

Virtus version: 1.0.5 Ruby version: ruby 2.2.2p95

I would expect attributes to not return the birthday As I am using virtus for form object it causes me problem when I want to do PATCH request

hl commented 8 years ago

Ruby

user.attributes.delete_if { |k, v| v.nil? }

Rails

user.attributes.compact
alaibe commented 8 years ago

Thx for your answer.

Unfortunately it is not enough => you can't set a value to nil with this solution

I already have a workaround to make it works (I can post it if you want)

My question is more: The README is not sync with the behaviour so if

booch commented 8 years ago

It'd take some effort to add tracking of which attributes have been set to nil and which have not been set. So I'm going to fix the README to match the behavior.