Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何仅更新Hanami模型中更改的属性?_Ruby_Hanami_Hanami Model - Fatal编程技术网

Ruby 如何仅更新Hanami模型中更改的属性?

Ruby 如何仅更新Hanami模型中更改的属性?,ruby,hanami,hanami-model,Ruby,Hanami,Hanami Model,鉴于我使用的是Hanami模型版本0.6.1,我希望存储库只更新实体的更改属性 例如: user_instance1 = UserRepository.find(1) user_instance1.name = 'John' user_instance2 = UserRepository.find(1) user_instance2.email = 'john@email.com' UserRepository.update(user_instance1) #expected: UPDATE

鉴于我使用的是Hanami模型版本0.6.1,我希望存储库只更新实体的更改属性

例如:

user_instance1 = UserRepository.find(1)
user_instance1.name = 'John'

user_instance2 = UserRepository.find(1)
user_instance2.email = 'john@email.com'

UserRepository.update(user_instance1)
#expected: UPDATE USER SET NAME = 'John' WHERE ID = 1

UserRepository.update(user_instance2)
#expected: UPDATE USER SET EMAIL = 'john@email.com' WHERE ID = 1
# will return a new account entity with updated attributes
>> Account.new(**account, name: 'A new one')
但实际情况是,第二个命令覆盖所有字段,包括未更改的字段

我知道我可以使用
Hanami::Entity::DirtyTracking
获取所有更改的属性,但我不知道如何使用这些属性部分更新实体


有办法做到这一点吗?

hanami实体是一个不可变的数据结构。这就是为什么不能使用setter更改数据:

>> account = AccountRepository.new.first
=> #<Account:0x00007ffbf3918010 @attributes={ name: 'Anton', ...}>

>> account.name
=> "Anton"

>> account.name = "Other"
        1: from /Users/anton/.rvm/gems/ruby-2.5.0/gems/hanami-model-1.2.0/lib/hanami/entity.rb:144:in `method_missing'
NoMethodError (undefined method `name=' for #<Account:0x00007ffbf3918010>)
此外,您还可以对旧实体对象使用
#update

>> AccountRepository.new.update(account.id, **account, name: 'A new name')
=> #<Account:0x00007ffbf3918010 @attributes={ name: 'Anton', ...}>

>> account = AccountRepository.new.first
=> #<Account:0x00007ffbf3918010 @attributes={ name: 'Anton', ...}>

>> account.name
=> "A new name"
>AccountRepository.new.update(account.id,**account,name:'A new name')
=> #
>>account=AccountRepository.new.first
=> #
>>account.name
=>“新名称”

您无法升级到v0.7.0的具体原因是什么?通过这种方式,您可以使用带有要更新的id和数据的更新。该软件是一个巨大的整体,升级并不容易,因为此升级需要处理一些新概念,例如不可变实体。我们正在努力解决这个问题,但我想知道是否有其他办法解决这个问题。解决办法是升级。