4

似乎我已經定義了源,因爲AR錯誤消息建議,但仍然出現錯誤。任何想法? Rails的3.2和Ruby 1.9.2Rails有許多通過錯誤

class Document < ActiveRecord::Base 
     has_many :participations 
     has_many :users, :through => :participations 

     has_many :editing_sessions 
     has_many :editors, :through => :editing_sessions, :source => :users 
    end 


    class User < ActiveRecord::Base 
     has_many :participations 
     has_many :documents , :through => :participations 

     has_many :editing_sessions 
     has_many :open_documents, :through => :editing_sessions, :source => :documents 
    end 


    class EditingSession < ActiveRecord::Base 
     belongs_to :users 
     belongs_to :documents 
    end 

    create_table "editing_sessions", :force => true do |t| 
     t.integer "user_id" 
     t.integer "document_id" 

     t.datetime "created_at", :null => false 
     t.datetime "updated_at", :null => false 
    end 


    Console: 

    u = User.first 
    => ... OK 

    u.editing_sessions 
    => [] 

    u.open_documents 
    => ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :document in model EditingSession. Try 'has_many :open_documents, :through => :editing_sessions, :source => <name>'. Is it one of :users or :documents? 

回答

3

嘗試改變EditingSession定義,以便belongs_to的標籤是在單數形式:

class EditingSession < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :document 
end 

但保留在文檔和用戶類的其他源定義複數形式(即:source => :users:source => :documents

那在Ruby on Rails Has-Many-Through Guide

+0

公約謝謝,該W作爲問題!這對我來說並不明顯。我錯過了什麼邏輯? – aaandre 2012-03-21 01:38:19

+0

由於在Documents類中的關聯是一個「has_many」欄假設它應該是複數形式,這就是爲什麼它應該是source =>:documents。但是,由於EditingSession使用的是「belongs_to」,它只能屬於一個文檔,所以rails假定應該有一個名爲「:document」的字段。 – plainjimbo 2012-03-21 01:41:48

+0

此外,在錯誤中,它顯示「無法找到源協會:文檔」。整個大會需要一點時間適應。 – plainjimbo 2012-03-21 01:43:24