加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails有很多错误

发布时间:2020-12-17 02:17:31 所属栏目:百科 来源:网络整理
导读:似乎我已经将源定义为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 =
似乎我已经将源定义为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,: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,:source => <name>'. Is it one of :users or :documents?

解决方法

尝试更改EditingSession定义,以便belongs_to标签采用单数形式:

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

但是以复数形式将其他源定义保留在Document和Users类中(即:source =>:users和:source =>:documents)

这是Ruby on Rails Has-Many-Through Guide的惯例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读