gpt4 book ai didi

ruby-on-rails - rails : In Rails how to use the model’s Attribute API on a tableless model

转载 作者:行者123 更新时间:2023-12-03 14:01:31 27 4
gpt4 key购买 nike

我有一个像这样的无表模型:

class SomeModel
include ActiveModel::Model
attribute :foo, :integer, default: 100
end
我正在尝试使用以下链接中的属性,它在普通模型中完美运行,但我无法在无表模型中使用它。
https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html
这会导致未定义
我试过添加事件记录属性:
include ActiveRecord::Attributes
作为一个包含但是这会导致与模式相关的不同错误。
如何在无表模型中使用该属性?谢谢。

最佳答案

您需要包含 ActiveModel::Attributes

class SomeModel
include ActiveModel::Model
include ActiveModel::Attributes
attribute :foo, :integer, default: 100
end

出于某种原因,它没有包含在 ActiveModel::Model 中.这个内部 API 是从 Rails 5 中的 ActiveRecord 中提取出来的,因此您可以将它与无表模型一起使用。

请注意 ActiveModel::Attributes不是 ActiveRecord::Attributes 相同. ActiveRecord::Attributes是一种更专业的实现,它假设模型由数据库模式支持。

关于ruby-on-rails - rails : In Rails how to use the model’s Attribute API on a tableless model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55598357/

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