Rails 错误未初始化常量Assignment::AssignmentsCourse 中的Has Many Through 关联

发布于 2025-01-01 03:05:11 字数 861 浏览 2 评论 0原文

我收到错误“未初始化常量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

AssignmentCourse.rb

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

I'm getting the error "uninitialized constant Assignment::AssignmentsCourse". Here are my models:


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

AssignmentCourse.rb

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-01-08 03:05:11
has_many :assignmentsCourses

这个字段和所有字段都不应该是驼峰式大小写,它不是 ruby​​ 风格,并且会破坏类加载。结尾也只能是复数形式,而不是两个词。在幕后,activerecord 会取消您提供的符号的复数,并执行类似于 require 的类加载。例如,如果您尝试 require 'activeRecord' 则不起作用。 Ruby 使用下划线来派生多单词类名称。

应该是:
has_many :assignment_courses

也可以更改 has much 。你的访问器不应该是驼峰式的,或者 ruby​​_style_is_to_underscore 。

has_many :assignmentsCourses

This and all of your fields should not be camel cased it is not ruby style and it breaks the class loading. The end should only be pluralized too, not both words. Behind the scenes activerecord depluralizes the symbol you provide and does class loading similar to require. If you tried require 'activeRecord' that would not work for example. Ruby uses underscores to derive multi word class names.

It should be:
has_many :assignment_courses

Change the has many though too. Your accessors should not be camel cased either ruby_style_is_to_underscore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文