【问题标题】:Rails 3 `has_one` :through getting undefined method `update_attributes'Rails 3`has_one`:通过获取未定义的方法`update_attributes'
【发布时间】:2012-01-10 17:13:56
【问题描述】:

我有一个模型,其中有用户、课程,我通过角色模型将两者连接起来。一切似乎都运行良好,我可以分配和检索学生,当我为课程分配教师时,我可以检索教师。我收到一个错误

Course.last.teacher
# => <# User username: 'schneems' ...>
course = Course.new
course.students = User.last(3)
# => [<# User ... >, <# User ... >, <# User ... >]
course.teacher = User.first
# => NoMethodError: undefined method 'update_attributes' for #<Class:0x007f86b29ee838>

这是我的基本模型

用户

class User < ActiveRecord::Base
  has_many :roles
  has_many :courses, :through => :roles
end

角色

class Role < ActiveRecord::Base
  belongs_to :user
  belongs_to :course
end

课程

class Course < ActiveRecord::Base
  has_many  :roles
  has_one   :teacher,  :through => :roles, :source => :user, :conditions => ['roles.name = ?', 'teacher']
  has_many  :students, :through => :roles, :source => :user, :conditions => ['roles.name = ?', 'student']
end

错误

完整的堆栈跟踪如下所示:

NoMethodError: undefined method `update_attributes' for #<Class:0x007f86b29ee838>
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/base.rb:1014:in `method_missing'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:444:in `block in method_missing'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/base.rb:1127:in `with_scope'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:440:in `method_missing'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/has_one_through_association.rb:22:in `create_through_record'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations/has_one_through_association.rb:10:in `replace'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/activerecord-3.0.9/lib/active_record/associations.rb:1465:in `block in association_accessor_methods'
    from (irb):43
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
    from /Users/schneems/.rvm/gems/ruby-1.9.2-p290@hourschool/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

有谁知道我怎样才能让这个关联发挥作用,我可以按预期检索和设置课程的教师属性?使用 Rails 3.0.9

【问题讨论】:

  • 我不知道是不是问题,但您只有User &lt;-&gt; Role 关联的一侧。你需要has_many :roles in User
  • 已更新,没有任何改变

标签: ruby-on-rails ruby activerecord


【解决方案1】:

如果您在 Rails 3.1 上运行此确切代码,您将收到以下错误消息:

ActiveRecord::HasOneThroughCantAssociateThroughCollection: 不能有 一个 has_one :through 关联 'Course#teacher' 其中 :through 关联“Course#roles”是一个集合。指定 has_one 或 :through 选项中的 belongs_to 关联。

此处使用的正确关联是 belongs_to,但在这种情况下,您不能将其放入角色表中。

问候丹尼尔

【讨论】:

  • 真可惜,这是一个方便的功能。所以你是说让这项工作的唯一方法是在课程模型中添加一个teacher_id,或者允许课程有多个老师?
【解决方案2】:

尝试将:class_name =&gt; "User" 添加到Course 上的:teacher 协会。

【讨论】:

  • 这不起作用NoMethodError: undefined method update_attributes' `
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
相关资源
最近更新 更多