【问题标题】:Rails with MongoID Embedded Doc No Mapping To My Model带有 MongoID 嵌入式文档的 Rails 没有映射到我的模型
【发布时间】:2016-12-11 18:56:27
【问题描述】:

这可能是我自己的一个问题我一直在努力通过一些 Rails 和 mongo 示例来加快速度(这都是为了学习而不是为了工作。)这个当前的项目我从一个 api 得到一个 JSON 字符串,我把它放在mongo中。所以改变文档结构并不是一个真正的选择。然后我一直在尝试将其映射到 Rails 模型中,以便我可以使用数据。

JSON 是一个包含许多事务的报告,其中包含许多 LogLines。

来自文档的片段

    < Report _id: 583 c3baac0baf90a7ee26f6e,
  ReportName: "PtSomIntPerfThreeLevelTest-1480341940187", 
  TestName: "PtSomIntPerfThreeLevelTest", 
  Transactions: [{
    "colId" => "50437d6c-49c1",
    "InfoPlate" => "[0SXdokPL-R13VQZwi]",
    "rowId" => "1",
    "sortDate" => 1480341975952,
    "transactionType" => "REQUEST",
    "description" => "description text for my document",
    "startDate" => "11/28/2016 14:06:15",
    "startDateMS" => 1480341975952,
    "endDate" => "11/28/2016 14:06:23",
    "endDateMS" => 1480341983069,
    "finalEndDate" => "11/28/2016 14:06:23",
    "finalEndDateMS" => 1480341983069,
    "completeDuration" => "7 seconds",
    "completeDurationMS" => 7117,
    "feedProcessingDuration" => "7 seconds",
    "feedProcessingDurationMS" => 7117,
    "logLines" => [{
        "id" => "1062b1ca-0f04",
        "timestamp" => 1480341975952,
        "transactionType" => "REQUEST",
        "transactionStep" => "RECEIVE",
        "collationId" => "50437d6c-49c1-438a-9b8",
        "runName" => "runName-1480341940187",
        "msg" => "Import default",
        "elapsedSeconds" => "0.0",
        "elapsedMS" => 0,
        "InfoPlate" => "[0SXdokPL-3rmxW3oH]"
    },

我有一个报告模型和一个事务模型(我将在一次执行 1 后执行 LogLines)我的报告模型很好,我可以根据 ID 获取单个报告文档并返回报告。然后我可以执行“report.transactions”并获取交易的 json blob(报告中几乎总是多个交易)但它不被识别为交易模型(将在下面发布所有代码)所以我不能说 transaction.InfoPlate 我得到一个没有这样的方法错误。我在我的模型中有关系,但我也有一个“字段:事务,类型:数组”,它在查看 mongoid 上的导轨时并不存在。没有它,我什么也得不到,所以关系“embeds_many:transactions”不允许我获得report.transaction。抱歉,如果这让我感到困惑,我的 Rails 术语很低。我想得到一份报告然后得到交易并能够进行 transactions.ColID 并获得 col ID 。

我的目标是为文档报告、事务、LogLines 的每个部分建立一个模型。我似乎不明白该怎么做。

报告模型(工作正常)

class Report
  include Mongoid::Document
  field :ReportName, type: String
  field :TestName, type: String
  field :Transactions, type: Array
  field :ReportDurationMS, type: String
  embeds_many :transactions
end

交易模型

class Transaction
  include Mongoid::Document
  field :colId, type: String
  field :InfoPlate, type: String
  field :rowId, type: String
  field :sortDate, type: String
  field :transactionDate, type: String
  field :description, type: String
  field :startDate, type: String
  field :startDateMS, type: Integer
  field :endDate, type: String
  field :endDateMS, type: Integer
  field :finalEndDate, type: String
  field :completeDuration, type: String
  field :completeDurationMS, type: Integer
  field :feedProcessingDuration, type: String
  field :feedProcessingDurationMS, type: Integer
  field :logLines, type: Array
  embedded_in :report, :inverse_of => :transactions
end

报告控制器(索引方法)调试记录器就在那里,而我到处乱搞

  def index
    @reports = Report.all
    Rails.logger.info("********* #{@reports.inspect} ***********")
  end

事务控制器(这是我无法将事务作为模型返回的内容)我从 @report.transactions 得到一个事务,但它只是一个 json 字符串,而不是 ruby​​ 模型。或者至少我不能调用像@transaction.colId 这样的东西。只是不返回这样的方法。 Transactions 是一个数组,有很多所以我确实尝试了 transactions.first.InfoPlate 但在我看来 Rails 似乎只是作为 JSON 字符串而不是对象返回的事务。?

    class TransactionsController < ApplicationController
  before_action :set_transaction, only: [:show, :edit, :update, :destroy]

  def index
    @report = Report.find(params[:report_id])
    @transaction = @report.transactions
    Rails.logger.info("********* report #{@report.inspect} ***********")


    @transactions = @report.transactions
    Rails.logger.info("********* transaction #{@transactions.inspect} ***********")

  end

我的路线

resources :reports do
   resources :transactions
 end

【问题讨论】:

  • field :Transactions, type: Array 让 Mongoid 知道有一个 Transactions 字段,embeds_many :transactions 告诉 Mongoid 应该有一个 transactions 数组,其元素是散列并且这些元素应该包含在 @ 987654331@ 对象。一切都区分大小写。顺便说一句,仅仅因为 API 为您提供了 JSON 数据块,并不意味着您不能或不应该重新排列/重命名事物以更好地适应您的应用程序。
  • 根据我上面的代码,我将如何获得交易的价值?例如,报告中任何给定交易的“transactionType”。我可以看到 Rails 知道我的报告是一个报告对象,我认为它不知道我的事务是一个事务对象。我想我是他们的 95%,我只是无法到达终点区
  • 好的,我根据您的帖子返回并清理了我的情况。对于我认为使用 Transactions 我现在可以使用的交易,我有小写的 t。我以为我在遵循 Rails 约定,但显然是在混合我的东西。通过并将所有“embeds_many :transactions”更改为 embeds_many :Transactions”谢谢

标签: ruby-on-rails mongoid


【解决方案1】:

上面的帖子让我回去匹配我的所有情况,这似乎有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多