1

我对 mogoid 和嵌入式文档感到非常恼火。我找不到创建、编辑或销毁它们的正确方法。我已经尝试了我在网络上找到的所有方法,但它们似乎都不起作用。

我还在 Rails 控制台上尝试了许多测试,但是仍然没有结果......我所做的所有测试都在数据库中保持不变。

这是主要课程:

class Boxer
  include Mongoid::Document
  include Mongoid::Paperclip

  store_in collection: "Boxers"

  field :NumeroLof
  field :Couleur
  field :DateNaissance
  .... many fields ...
  field :Contacts
  field :PublicationEtalon

  has_mongoid_attached_file :Image
  validates_attachment_content_type :Image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

  embeds_many :Certificats, as: :Certificat
  accepts_nested_attributes_for :Certificats, allow_destroy: true
end

拳击手对象应如下所示:

{
"_id" : ObjectId("584676a482ed3c1bc77484b3"),
"NumeroLof" : 1316Ae024,
"Name" : "SHEBA",
"Affixe" : "DU JABELIN",
"DateNaissance" : ISODate("2001-05-24T00:00:00.000Z"),
"Sexe" : "F",
"Couleur" : "B",
"Identification" : "2AXZ279",
"Certificats" : [ 
    {
        "Exposition" : "EYRAGUES NE",
        "Date" : ISODate("2011-06-26T00:00:00.000Z"),
        "Juge" : "P. Asensi",
        "Observation" : "",
        "Denomination" : "M-VET",
        "_id" : ObjectId("58467f1082ed3c1bc77557b3")
    }, 
    {
        "Exposition" : "PRADINES RE",
        "Date" : ISODate("2011-08-14T00:00:00.000Z"),
        "Juge" : "R. Pras",
        "Observation" : "",
        "Denomination" : "M-VET",
        "_id" : ObjectId("58467f1082ed3c1bc77557bd")
    }
]

}

最后,这是我的 CertificatsController:

class CertificatsController < ApplicationController
      before_action :load_boxer
      before_action :load_certificat, only: [:show, :edit, :update, :destroy]

      # GET /boxer/:id/certificates/
      def index
        @certificats = @boxer.Certificats
      end

      # GET /boxer/:id/certificates/:id/
      def show
      end

      # GET /boxer/:id/certificates/new
      def new
        @certificat = @boxer.Certificat.new
      end

      # POST /boxer/:id/certificates/
      def create
        @certificat = Certificat.build(certificat_params)
        if @certificat.save
          @boxer.Certificats.new(@certificat)
          @boxer.reload
          redirect_to boxer_certificats_path(@boxer), notice: "Nouveau certificat créé avec succès" and return
        end
        render 'new'
      end

      def edit
      end

      def update
        if @certificat.update_attributes(certificat_params)
          @boxer.reload
          redirect_to boxer_certificats_path(@boxer), notice: "le certificat a été mis à jour!" and return
        end

        render 'edit'
      end

      def destroy
        @certificat.destroy
        @boxer.reload
        redirect_to boxer_certificats_path, notice: "le certificat a été supprimé!" and return
      end

      private
      def certificat_params
        params.require(:certificat).permit(:Denomination, :Date, :Exposition, :Juge,
        :Observation)
      end

      def load_boxer
        @boxer = Boxer.find(params[:boxer_id])
      rescue Mongoid::Errors::DocumentNotFound
        not_found
      end

      def load_certificat
        @certificat = @boxer.Certificats.find(params[:id])
      end

    end

实际上,我对 index 和 show 方法没有任何问题,但是所有 CRUD 操作都失败了,我的查询没有持久性。

提前感谢您的回复。

4

2 回答 2

0

我发现了我自己犯的错误。对于那些可能想知道的人,重点是:在 Certificate 类中,embedded_in Boxer需要 a ...

于 2017-01-17T13:54:27.903 回答
0

自从我遇到一个听起来非常相似的问题以来已经有一段时间了。我想知道对象是否没有被标记为脏/已更改。他们的方式我总是解决这个问题。

class Boxer
  embeds_many :Certificats, as: :Certificat
end

b = Boxer.new
b.certificats_will_change!
b.certificats = Certifcat.new
b.save!
于 2016-12-27T17:49:56.913 回答