簡體   English   中英

Ruby on Rails多個關聯

[英]Ruby on Rails multiple associations

我嘗試制作一個應用程序,其中一個用戶(呼叫客戶端)放置了一個文檔,並使其被另一個用戶(呼叫校正器)更正。

我有一個經典的用戶表,我有一個文檔表和更正表,當客戶決定將其文件發送給更正者時,它將文檔復制到更正者帳戶中。

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

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中到達更正的索引頁面時:

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

您的User模型和Document模型對我來說似乎很奇怪。為什么在cclientccorector中使用c呢? 並且您mispelledcorector (應該是校正器) 對於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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM