Ruby on Rails多个关联

时间:2014-06-04 20:04:09

标签: ruby-on-rails ruby associations activeadmin

我尝试创建一个应用程序,其中用户(调用客户端)放置文档并由另一个用户(调用纠正器)更正。

我有一个经典的User表,我有一个Document Table和Correction表 当客户决定向纠正者发送他的文件时,它会将文件复制到校正者帐户中。

在我的用户模型中,我有:

has_many :documents
has_many :cclient,    :class_name => 'Correction', :foreign_key => 'client_id'
has_many :ccorector,  :class_name => 'Correction', :foreign_key => 'corrector_id'

在我的文档模型中,我有:

belongs_to :user
has_one  :cclient,    :class_name => 'Correction', :foreign_key => 'client_document_id'
has_one  :ccorrector, :class_name => 'Correction', :foreign_key => 'corrector_document_id'

最终在我的修正模型中我有:

belongs_to :client,             :class_name => 'User',  :foreign_key => "client_id"
belongs_to :corrector,          :class_name => 'User',  :foreign_key => "corrector_id"
belongs_to :client_document,    :class_name => 'Document', :foreign_key => "client_document_id"
belongs_to :corrector_document, :class_name => 'Document', :foreign_key => "corrector_document_id"

我的问题是当我尝试在ActiveAdmin中访问Correction的索引页面时,我在日志中看到:

User Load (0.5ms)  SELECT "users".* FROM "users"
CACHE (0.0ms)  SELECT "users".* FROM "users" 
Document Load (0.6ms)  SELECT "documents".* FROM "documents" 
CACHE (0.0ms)  SELECT "documents".* FROM "documents"

我非常确定生产中的原因是否达到了超时。

我哪里错了?

编辑:这里是我的correction.rb(在活跃的管理员中)

#encoding: utf-8
ActiveAdmin.register Correction do
  config.per_page = 10
  index do
    column :id
    default_actions
  end


  form do |f|
    f.inputs "Correction" do
      f.input :client_id
    end
    f.actions
  end
end

1 个答案:

答案 0 :(得分:2)

你的 User 模型和 Document 模型对我来说似乎很奇怪。为什么你前面有 c < / strong>在 cclient ccorector ?并且你 mispelled corector(应该是纠正器)。对于 has_many 关系,您应该使用 plural form 而不是 singular form

我猜您的 User 模型和 Document 模型应该如下所示

#user.rb
class User < ActiveRecord::Base

has_many :documents
has_many :clients,    :class_name => 'Correction', :foreign_key => 'client_id'
has_many :correctors, :class_name => 'Correction', :foreign_key => 'corrector_id'

end

#document.rb
class Document < ActiveRecord::Base

belongs_to :user
has_one  :client,    :class_name => 'Correction', :foreign_key => 'client_document_id'
has_one  :corrector, :class_name => 'Correction', :foreign_key => 'corrector_document_id'

end