rails admin和has_one包括一个模型两次

时间:2015-02-09 08:24:24

标签: ruby-on-rails-4 mongoid rails-admin

我有两次使用has_one关系包含其他模型的模型。我需要能够通过rails_admin中的下拉列表选择每个。有我的方式:

class Unit
  include Mongoid::Document
  has_one :quiz, class_name: 'Quiz', inverse_of: :quiz_socket, dependent: :destroy
  has_one :case, class_name: 'Quiz', inverse_of: :case_socket, dependent: :destroy
  def quiz_socket_id
   self.quiz.try :q_id
  end
  def quiz_socket_id=(q_id)
   self.quiz = Quiz.find(q_id)
  end
  def case_socket_id
   self.case :c_id
  end
  def case_socket_id=(c_id)
   self.case = Quiz.find(c_id)
  end
end

测验模型:

class Quiz
  include Mongoid::Document

  field :name

  belongs_to :quiz_socket, class_name: 'Unit', inverse_of: :quiz
  belongs_to :case_socket, class_name: 'Unit', inverse_of: :case

  validate :only_one_socket_present

  protected
  def only_one_socket_present
     errors[:case_socket] << 'only one socket can be presented' if quiz_socket.present? && case_socket.present?
  end
end

和rails_admin:

RailsAdmin.config do |config|
  config.model Unit do
    edit do
     field :quiz, :belongs_to_association
     field :case, :belongs_to_association
    end
  end
end

它几乎可以工作。我可以选择测验和案例,但保存后,单位只有测验,案例从数据库中删除。

有服务器日志:

> Processing by RailsAdmin::MainController#edit as HTML   Parameters:
> {"utf8"=>"✓",
> "authenticity_token"=>"/N9PXwsmmuNrjhprml/QR5qxtsejovf+yl4A2fqnes4=",
> "unit"=>{"summary"=>"<p>edh</p>\r\n", "name"=>"sdg", "video_link"=>"",
> "part_id"=>"54d4c680616e7444bf070000", "unit_progress_ids"=>["", ""],
> "quiz_socket_id"=>"54d866a4616e741381030000",
> "case_socket_id"=>"54d8698e616e7414cc000000"}, "return_to"=>"",
> "_save"=>"", "model_name"=>"unit", "id"=>"54d4d446616e744b00030000"}  
> MOPED: 127.0.0.1:27017 COMMAND      database=admin
> command={:ismaster=>1} runtime: 2.7070ms   MOPED: 127.0.0.1:27017
> QUERY        database=samolov_development collection=admins
> selector={"$query"=>{"_id"=>BSON::ObjectId('54cb781b616e744c09000000')},
> "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil
> fields=nil runtime: 3.6702ms   MOPED: 127.0.0.1:27017 QUERY       
> database=samolov_development collection=units
> selector={"_id"=>BSON::ObjectId('54d4d446616e744b00030000')} flags=[]
> limit=0 skip=0 batch_size=nil fields=nil runtime: 2.9342ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=unit_progresses selector={"_id"=>nil} flags=[] limit=0
> skip=0 batch_size=nil fields=nil runtime: 2.9588ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=unit_progresses selector={"_id"=>nil} flags=[] limit=0
> skip=0 batch_size=nil fields=nil runtime: 1.7743ms   MOPED:
> 127.0.0.1:27017 UPDATE       database=samolov_development collection=unit_progresses
> selector={"unit_id"=>BSON::ObjectId('54d4d446616e744b00030000'),
> "_id"=>{"$nin"=>[]}} update={"$set"=>{"unit_id"=>nil}} flags=[:multi]
>                          COMMAND      database=samolov_development command={:getlasterror=>1, :w=>1} runtime: 4.1075ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=quizzes
> selector={"_id"=>BSON::ObjectId('54d866a4616e741381030000')} flags=[]
> limit=0 skip=0 batch_size=nil fields=nil runtime: 3.9158ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=quizzes
> selector={"$query"=>{"quiz_socket_id"=>BSON::ObjectId('54d4d446616e744b00030000')},
> "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil
> fields=nil runtime: 4.1857ms   MOPED: 127.0.0.1:27017 QUERY       
> database=samolov_development collection=questions
> selector={"quiz_id"=>BSON::ObjectId('54d866a4616e741381030000')}
> flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 2.8849ms  
> MOPED: 127.0.0.1:27017 DELETE       database=samolov_development
> collection=quizzes
> selector={"_id"=>BSON::ObjectId('54d866a4616e741381030000')}
> flags=[:remove_first]
>                          COMMAND      database=samolov_development command={:getlasterror=>1, :w=>1} runtime: 3.5143ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=questions
> selector={"quiz_id"=>BSON::ObjectId('54d866a4616e741381030000')}
> flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 1.9553ms  
> MOPED: 127.0.0.1:27017 QUERY        database=samolov_development
> collection=quizzes
> selector={"_id"=>BSON::ObjectId('54d8698e616e7414cc000000')} flags=[]
> limit=0 skip=0 batch_size=nil fields=nil runtime: 2.6045ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=quizzes
> selector={"$query"=>{"case_socket_id"=>BSON::ObjectId('54d4d446616e744b00030000')},
> "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil
> fields=nil runtime: 3.0093ms   MOPED: 127.0.0.1:27017 UPDATE      
> database=samolov_development collection=quizzes
> selector={"_id"=>BSON::ObjectId('54d8698e616e7414cc000000')}
> update={"$set"=>{"case_socket_id"=>BSON::ObjectId('54d4d446616e744b00030000')}}
> flags=[]
>                          COMMAND      database=samolov_development command={:getlasterror=>1, :w=>1} runtime: 3.6337ms   MOPED:
> 127.0.0.1:27017 QUERY        database=samolov_development collection=questions
> selector={"quiz_id"=>BSON::ObjectId('54d8698e616e7414cc000000')}
> flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 1.9600ms  
> MOPED: 127.0.0.1:27017 QUERY        database=samolov_development
> collection=parts
> selector={"$query"=>{"_id"=>BSON::ObjectId('54d4c680616e7444bf070000')},
> "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil
> fields=nil runtime: 7.6532ms

0 个答案:

没有答案