gpt4 book ai didi

ruby-on-rails - Rails 有很多直通错误

转载 作者:行者123 更新时间:2023-12-02 00:48:08 25 4
gpt4 key购买 nike

似乎我已经按照 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?

最佳答案

尝试更改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 中的约定

关于ruby-on-rails - Rails 有很多直通错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9797367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com