【问题标题】:Rails Error uninitialized constant Assignment::AssignmentsCourse in Has Many Through AssociationRails 错误未初始化常量Assignment::AssignmentsCourse in Has Many Through Association
【发布时间】:2012-01-29 02:56:57
【问题描述】:

我收到错误“未初始化的常量 Assignment::AssignmentsCourse”。这是我的模型:


assignment.rb

class Assignment < ActiveRecord::Base
    has_many :assignmentsCourses
    has_many :courses, :through => :assignmentsCourses
    attr_accessible :name, :dateAssigned, :dateDue, :description, :weight, :category_tokens
    attr_reader :category_tokens

    def category_tokens=(ids)
        puts 'el ids: ', ids.split(",")
        self.courseIds = ids.split(",")
    end
end

course.rb

class Course < ActiveRecord::Base
    has_and_belongs_to_many :assignments
end

作业课程.rb

class AssignmentCourse < ActiveRecord::Base
    belongs_to :assignment
    belongs_to :course
    attr_accessible :assignment_id, :course_id
end

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord has-many-through nameerror


    【解决方案1】:
    has_many :assignmentsCourses
    

    这个和你所有的字段不应该是驼峰式的,它不是 ruby​​ 样式,它会破坏类加载。结尾也只能是复数形式,而不是两个词。在幕后,activerecord 将您提供的符号去复数,并进行类似于require 的类加载。例如,如果您尝试过require 'activeRecord',那将无法正常工作。 Ruby 使用下划线来派生多词类名。

    应该是: has_many :assignment_courses

    改变也有很多。 ruby_style_is_to_underscore 的访问器不应该是驼峰式的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2011-04-13
      相关资源
      最近更新 更多