简体   繁体   中英

Ruby on Rails multiple associations

I try to make an application where a user (call client) put a document and make it corrected by another user (call corrector).

I have a classic User table, I have a Document Table and Correction table When the client decide to send to a corrector his file, it will duplicate the Document into the corrector account.

In my User model I have:

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

In my Document model I have:

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'

Finaly in my Correction model I have:

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"

My problem is when i try to reach the index page of Correction in ActiveAdmin I see in my log:

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"

And i'm pretty sure that the reason why in production i reach a timeout.

Where am I wrong ?

EDIT: Here my correction.rb (in active admin)

#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

Your User model and Document model seems weird to me.Why you are having preceding c in cclient and ccorector ? and you mispelled the corector (it should be corrector) . And for has_many relation,you should be using plural form not singular form .

I guess your User model and Document model should look like this

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM