【问题标题】:rails - NameError (undefined local variable or method while using has_many :throughrails - NameError(使用 has_many 时未定义的局部变量或方法:通过
【发布时间】:2015-06-23 18:58:07
【问题描述】:

我的 rails 应用程序出现以下错误:

NameError(未定义的局部变量或方法 'fac_allocs' for #):
app/models/room.rb:4:in ''
app/models/room.rb:1:in ''
app/controllers/rooms_controller.rb:3:in 'index'

room.rb文件

class Room < ActiveRecord::Base
  has_many :bookings
  has_many :fac_allocs
  has_many :facs, :through => fac_allocs
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    在您的 associations 而不是 fac_allocs 中,您需要使用符号,因为 rails 正在查找名为 fac_allocs 而不是您的关联模型的变量或方法

    has_many :facs, through: :fac_allocs
    

    【讨论】:

      【解决方案2】:

      将代码更改为:

      class Room < ActiveRecord::Base
        has_many :bookings
        has_many :fac_allocs
        has_many :facs, :through => :fac_allocs
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多