ruby-on-rails - 没有自己的表的 Rails 嵌套模型与数据库之间的转换

标签 ruby-on-rails ruby database activerecord activemodel

背景:我正在阅读 Practical Object Oriented Design in Ruby 后尝试重构我的代码(太棒了),在这样做的过程中,我想引入更多封装责任的模型,而不是拥有一个包含逻辑(和 case 语句)的大文件。

问题:为了简化问题陈述,我有一个“有很多”RuleConditions 的模型Rule。但是,数据库中只有一张表用于规则。在其中,我有一个类型为 jsonb 的条件列(基于 RuleCondition 的复杂性)。但我似乎无法做到这一点。具体来说,我无法弄清楚如何使用嵌套模型实例化模型,并期望 ActiveRecord 知道如何将模型转换为 jsonb,并且可能从表返回到嵌套模型。我也不知道我是否可以使用 ActiveRecord 定义一个 has_many 关系而没有表支持它。

我的期望:

我希望应该有一些流程(由 ActiveRecord 和 ActiveModel 的混合定义)使这个流程成为可能

  1. 获取规则的参数。
  2. 创建一个新的 RuleConditions 数组 规则参数的子集。
  3. 执行 Rule.new(rule) 其中规则包含 :conditions => RuleCondition
  4. 执行rule.save!
  5. 稍后,从表中获取规则,并期望它使用来自 conditions 的嵌套 RuleConditions 模型重建一个 Rule > 属性。

我尝试过的:

我认为 serialize, :conditions, JSON 会让我半途而废,但它很难序列化我的对象。之后,我真的不知道了。我也玩过 ActiveModel::Conversion 。所以我只需要一些指导。

并且,非常清楚,在我的 RuleCondition 上调用 as_json 的工作方式与我预期的一样(打印出用于存储在 中的相同 JSON >Rule 模型和数据库,然后再尝试重构)。所以可能是我不明白 serialize(因为它应该是 YAML,除非另有说明,我认为编码不同于仅仅“匹配我的列类型”)

编辑:

目前我有类似的东西(准系统,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 毫秒)插入“规则”(“名称”、“条件”、“创建时间”、“更新时间”)值($1、$2、$3、$4)返回“id”[[“名称”、“wefw” ], ["条件", "{#}"], ["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 | 详细信息: token “#”无效。 18:13:52 web.1 |上下文:JSON 数据,第 1 行:#... 18:13:52 web.1 | :插入“规则”(“名称”、“条件”、“创建时间”、“更新时间”)值($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

不要对原生 JSON/JSONB 列使用 serialize。它旨在与字符​​串列一起使用,作为穷人的替代品。

您尝试做的实际上超出了 ActiveRecord 的范围 - AR 是围绕关系模型构建的,其中模型对应于表。你不能指望 AR 有任何规定可以将 JSONB 列解码为基本标量类型以外的任何类型。我会考虑你所做的是否真的值得付出努力,而不是实际为关系创建一个单独的表。

您使用 ActiveModel::Model 走在正确的 rails 上,这将使您的模型具有与常规模型相同的行为,但您应该看看如何 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

您可以通过创建 setter/getter 来模拟关联的其他方面。

但话又说回来,您可以只创建一个包含 JSONB 列和一对多或 m2m 关联的 rule_conditions 表,并将您的时间花在实际工作上。

关于ruby-on-rails - 没有自己的表的 Rails 嵌套模型与数据库之间的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799048/

相关文章:

ruby-on-rails - Rails 4 - 查找嵌套属性,选择某些子项

html - 将 CSS 元素加载到 Rails 应用程序中

ruby - 更喜欢 %w(...) 而不是文字数组?

php - Symfony2 : How to remove an element from a Doctrine ArrayCollection (many-to-many relation)?

iphone - 将图像存储到sqlite数据库中

java - 将 Android 应用程序连接到计算机上的数据库

ruby-on-rails - ruby on rails 语言问题 "invalid byte sequence in GBK"

ruby-on-rails - 在 `find_or_create_by` `has_many` 关联上使用 `through` 时出错

ruby - 如何在 ruby​​ 中获取绑定(bind)到变量的实例方法?

ruby-on-rails - 每当 gem 不自动运行?