Rails имеет много ошибок - PullRequest
       11

Rails имеет много ошибок

4 голосов
/ 21 марта 2012

Кажется, я определил источник, как подсказывает сообщение об ошибке AR, но все равно получаю ошибки. Любая идея? Rails 3.2, Ruby 1.9.2

    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?

1 Ответ

3 голосов
/ 21 марта 2012

Попробуйте изменить определение EditingSession, чтобы метки метки принадлежат к единственному виду:

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

Но оставьте определения других источников в классах Document и Users в форме множественного числа (т. Е. :source => :users и:source => :documents)

Это соглашение, содержащееся в Ruby on Rails Guide-Many-Through

...