gpt4 book ai didi

ruby - 更新属性集作为 DataMapper 中的键

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:29 24 4
gpt4 key购买 nike

如果 :key 设置为 true,是否可以更新 DataMapper 中的属性?

例如,我有一个这样设置的模型:

class Post
include DataMapper::Resource
property :slug, Text, :unique => true, :key => true
# ...
end

我用 :slug => "example-post-title" 创建了一个新实例。

我试图通过访问存储来更新它

@post = Post.get("example-post-title")
#=> #<Post @slug="example-post-title" ...>
@post.slug = "example-post-title-2"
#=> "example-post-title-2"
@post.save
#=> true
@post = Post.get("example-post-title-2")
#=> nil

但如您所见,该 slug 从未更新过。我也尝试使用 Post#update方法:

@post = Post.get("example-post-title")
#=> #<Post @slug="example-post-title" ...>
@post.update(:slug => "example-post-title-2")
#=> true
@post = Post.get("example-post-title-2")
#=> nil

在数据库中查看,这些示例中的任何一个都没有更改索引列。它仍然是 example-post-title 而不是 example-post-title-2

根据文档,Post#update方法,类似于 Post#save 方法,如果操作成功则返回 true,否则返回 false。它在这里返回 true,但实际上并没有更新记录。

我在网上找了又找,找不到任何关于它的资料。 StackOverflow 和 DataMapper rdoc 都没有关于更新 key 的任何内容。

我知道我可以拥有一个独特的 Serial 属性,并使用 slug 获取 Post 的实例(例如,制作 Serial property the key instead of the slug),但我正在寻找一种方法来做到这一点,如果可能的话。

最佳答案

我的预感是您无法更新 key 。根据文档,它们受到保护以防止批量分配:

Natural Keys are protected against mass-assignment, so their setter= will need to be called individually if you're looking to set them.

他们不谈论更新它们,但通常在“key => value”存储中更新 key 是不可能的或已弃用的。我假设这里也是这种情况,即使我找不到任何确凿的证据给你:/

关于ruby - 更新属性集作为 DataMapper 中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32302407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com