2016-12-20 6 views
1

私はMogoidと組み込み文書に非常に悩まされます。私はそれらを作成、編集、または破壊するための適切な方法を見つけることができません。私はウェブ上で見つけたすべての方法を試しましたが、どれもうまくいかないようです。Mongoid、埋め込み文書には永続性がありません

私はまた、レールコンソールで多くのテストを試みましたが、まだ結果はありません... 私がやったすべてのテストは、DBに固定されていません。ここで

はメインクラスです:

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 

実際、インデックスとショーのメソッドには問題はありませんが、すべてのCRUD操作は失敗します。クエリの永続性はありません。

返信いただきありがとうございます。

答えて

0

私は非常に似ている問題に遭遇して以来、しばらくしています。オブジェクトが汚れている/変更されているとしてマークされていない場合は私は思っています。私はいつもこれを回避していました。

class Boxer 
    embeds_many :Certificats, as: :Certificat 
end 

b = Boxer.new 
b.certificats_will_change! 
b.certificats = Certifcat.new 
b.save! 
0

私は自分で作った間違いを見つけました。疑問に思う人のために、ここにポイントがあります: 証明書クラスでは、embedded_in Boxerが必要でした...

関連する問題