Rails 没有自己的数据库的嵌套模型转换 table
Rails nested model conversion to and from database without its own table
背景:我在阅读 Practical Object Oriented Design in Ruby 后尝试重构我的代码(太棒了),在这样做的过程中,我想引入更多封装责任的模型,而不是一个大文件有逻辑(和案例陈述,为此)。
问题:为了简化问题陈述,我有一个模型 Rule
"has many" RuleConditions
。但是,规则的数据库中只有一个table。在其中,我有一个 jsonb
类型的条件列(基于 RuleCondition
的复杂性)。但我似乎无法做到这一点。具体来说 我不知道如何用嵌套模型实例化模型,并期望 ActiveRecord 知道如何将模型转换为 jsonb,也许从 table 返回到嵌套模型。我也不知道我是否可以定义一个 has_many
关系而没有 table 使用 ActiveRecord 支持它。
我的期望:
我希望应该有一些流程(由 ActiveRecord 和 ActiveModel 的混合定义)使这个流程成为可能
- 获取
Rule
的参数。
- 从
RuleConditions
创建一个新数组
规则的参数子集。
- 在规则包含 :conditions =>
RuleCondition
的地方执行 Rule.new(rule)
- 做
rule.save!
- 稍后,从 table 中获取规则,并期望它使用
conditions
属性中的嵌套 RuleConditions
模型重建 Rule
。
我尝试过的:
我认为 serialize, :conditions, JSON
会让我半途而废,但它很难序列化我的对象。之后,我真的不知道了。我也玩过 ActiveModel::Conversion 。所以我只需要一些指导。
而且,非常清楚,在我的 RuleCondition
上调用 as_json
就像我期望的那样(打印出与以前存储在 Rule
尝试重构之前的模型和数据库)。所以可能是我不明白 serialize
(因为它应该是 YAML,除非另有说明,我认为编码不同于 "match my column type")
编辑:
目前我有类似的东西(准系统,0 个验证/关联)
class Rule < ActiveRecord::Base
end
class RuleController < ApplicationController
def create
rule = Rule.new(rule_params[:rule]) # conditions are just an attribute in the params
rule.save
end
end
现在,新模型定义为
class RuleCondition
include ActiveModel::Model # (what I'm currently doing to get some of the behavior of a model without the persistence / table backing it, I think)
attr_accessor :noun, :subnoun # etc
end
我想我需要这样做
def create
rule = rule_params[:rule]
rule["conditions"] = rule["conditions"].map do |c|
RuleCondition.new(c)
end
true_rule = Rule.new(rule)
true_rule.save!
end
但这行不通,因为(正是)这个原因:
18:13:52 web.1 | SQL(10.7 毫秒)插入 "rules"("name"、"conditions"、"created_at"、"updated_at")值($1、$2、$3、$4 ) 返回 "id" [["name", "wefw"], ["conditions", "{#}"], ["created_at", "2018-12-16 02:13:52.938849"], ["updated_at", "2018-12-16 02:13:52.938849"]]
18:13:52 web.1 | PG::InvalidTextRepresentation:错误:类型 json 的输入语法无效
18:13:52 web.1 | 详细信息:令牌“#”无效。
18:13:52 web.1 |上下文:JSON 数据,第 1 行:#...
18:13:52 web.1 | : 插入 "rules" ("name", "conditions", "created_at", "updated_at") 值 ($1, $2, $3, $4) 返回 "id"
18:13:52 web.1 | (0.5 毫秒)回滚
Keep in mind that database adapters handle certain serialization tasks
for you. For instance: json and jsonb types in PostgreSQL will be
converted between JSON object/array syntax and Ruby Hash or Array
objects transparently. There is no need to use serialize in this case.
- api.rubyonrails.org
不要将 serialize
与本机 JSON/JSONB 列一起使用。它旨在与字符串列一起使用,作为穷人的替代品。
您尝试做的实际上超出了 ActiveRecord 的范围 - AR 是围绕关系模型构建的,其中模型对应于 tables。你不能指望 AR 有任何规定可以将 JSONB 列解组为基本标量类型以外的任何类型。我会考虑你正在做的事情是否真的值得付出努力,而不是实际为关系创建一个单独的 table。
你在 ActiveModel::Model 的正确轨道上,这将使你的模型具有与常规模型相同的行为,但你应该看看 ActiveRecord handles nested attributes:
class Rule < ApplicationRecord
def conditions_attributes=(attributes)
attributes.each do |a|
# you would need to implement this method
unless RuleCondition.reject_attributes?(a)
self.conditions << RuleCondition.new(c)
end
end
end
end
您可以通过创建 setters/getters 来模仿关联的其他方面。
但话又说回来,您可以创建一个带有 JSONB 列和一对多或 m2m 关联的 rule_conditions table,然后将您的时间实际用于提高工作效率。
背景:我在阅读 Practical Object Oriented Design in Ruby 后尝试重构我的代码(太棒了),在这样做的过程中,我想引入更多封装责任的模型,而不是一个大文件有逻辑(和案例陈述,为此)。
问题:为了简化问题陈述,我有一个模型 Rule
"has many" RuleConditions
。但是,规则的数据库中只有一个table。在其中,我有一个 jsonb
类型的条件列(基于 RuleCondition
的复杂性)。但我似乎无法做到这一点。具体来说 我不知道如何用嵌套模型实例化模型,并期望 ActiveRecord 知道如何将模型转换为 jsonb,也许从 table 返回到嵌套模型。我也不知道我是否可以定义一个 has_many
关系而没有 table 使用 ActiveRecord 支持它。
我的期望:
我希望应该有一些流程(由 ActiveRecord 和 ActiveModel 的混合定义)使这个流程成为可能
- 获取
Rule
的参数。 - 从
RuleConditions
创建一个新数组 规则的参数子集。 - 在规则包含 :conditions =>
RuleCondition
的地方执行 - 做
rule.save!
- 稍后,从 table 中获取规则,并期望它使用
conditions
属性中的嵌套RuleConditions
模型重建Rule
。
Rule.new(rule)
我尝试过的:
我认为 serialize, :conditions, JSON
会让我半途而废,但它很难序列化我的对象。之后,我真的不知道了。我也玩过 ActiveModel::Conversion 。所以我只需要一些指导。
而且,非常清楚,在我的 RuleCondition
上调用 as_json
就像我期望的那样(打印出与以前存储在 Rule
尝试重构之前的模型和数据库)。所以可能是我不明白 serialize
(因为它应该是 YAML,除非另有说明,我认为编码不同于 "match my column type")
编辑:
目前我有类似的东西(准系统,0 个验证/关联)
class Rule < ActiveRecord::Base
end
class RuleController < ApplicationController
def create
rule = Rule.new(rule_params[:rule]) # conditions are just an attribute in the params
rule.save
end
end
现在,新模型定义为
class RuleCondition
include ActiveModel::Model # (what I'm currently doing to get some of the behavior of a model without the persistence / table backing it, I think)
attr_accessor :noun, :subnoun # etc
end
我想我需要这样做
def create
rule = rule_params[:rule]
rule["conditions"] = rule["conditions"].map do |c|
RuleCondition.new(c)
end
true_rule = Rule.new(rule)
true_rule.save!
end
但这行不通,因为(正是)这个原因:
18:13:52 web.1 | SQL(10.7 毫秒)插入 "rules"("name"、"conditions"、"created_at"、"updated_at")值($1、$2、$3、$4 ) 返回 "id" [["name", "wefw"], ["conditions", "{#}"], ["created_at", "2018-12-16 02:13:52.938849"], ["updated_at", "2018-12-16 02:13:52.938849"]] 18:13:52 web.1 | PG::InvalidTextRepresentation:错误:类型 json 的输入语法无效 18:13:52 web.1 | 详细信息:令牌“#”无效。 18:13:52 web.1 |上下文:JSON 数据,第 1 行:#... 18:13:52 web.1 | : 插入 "rules" ("name", "conditions", "created_at", "updated_at") 值 ($1, $2, $3, $4) 返回 "id" 18:13:52 web.1 | (0.5 毫秒)回滚
Keep in mind that database adapters handle certain serialization tasks for you. For instance: json and jsonb types in PostgreSQL will be converted between JSON object/array syntax and Ruby Hash or Array objects transparently. There is no need to use serialize in this case.
- api.rubyonrails.org
不要将 serialize
与本机 JSON/JSONB 列一起使用。它旨在与字符串列一起使用,作为穷人的替代品。
您尝试做的实际上超出了 ActiveRecord 的范围 - AR 是围绕关系模型构建的,其中模型对应于 tables。你不能指望 AR 有任何规定可以将 JSONB 列解组为基本标量类型以外的任何类型。我会考虑你正在做的事情是否真的值得付出努力,而不是实际为关系创建一个单独的 table。
你在 ActiveModel::Model 的正确轨道上,这将使你的模型具有与常规模型相同的行为,但你应该看看 ActiveRecord handles nested attributes:
class Rule < ApplicationRecord
def conditions_attributes=(attributes)
attributes.each do |a|
# you would need to implement this method
unless RuleCondition.reject_attributes?(a)
self.conditions << RuleCondition.new(c)
end
end
end
end
您可以通过创建 setters/getters 来模仿关联的其他方面。
但话又说回来,您可以创建一个带有 JSONB 列和一对多或 m2m 关联的 rule_conditions table,然后将您的时间实际用于提高工作效率。