1

我正在关注为 Rails 3 制作的教程,我正在使用 Rails 3.2,但认为我正确地遵循它但出现错误,我的课程如下,当我使用 task.admin_users_tasks 时发生错误

class Task < ActiveRecord::Base

  attr_accessible :name, :project_id, :task_id, :permalink, :visible, :position, :created_at
  belongs_to :project
  has_and_belongs_to_many :admin_users
  has_many :admin_users_tasks
end

class AdminUsersTask < ActiveRecord::Base
  attr_accessible :admin_user_id, :task_id, :created_at, :updated_at
  belongs_to :admin_user, :class_name => "AdminUser" :foreign_key => 'admin_user_id'
  belongs_to :task
end

class AdminUser < ActiveRecord::Base

  attr_accessible :first_name, :last_name, :username, :email

  has_and_belongs_to_many :projects
  has_and_belongs_to_many :tasks
  has_many :admin_users_tasks
  has_many :admin_users_projects
end

Rails 控制台

    1.9.3-p362 :032 > task
 => #<Task id: 1, project_id: 6, permalink: "taskytask", position: 1, visible: true,   created_at: "2013-02-21 05:08:01", updated_at: "2013-02-21 05:08:01", name: "Task Name for   Noname Project"> 
1.9.3-p362 :033 > me
 => #<AdminUser id: 1, first_name: "Larry", last_name: "David", email:         "larry.david@mail.com", hashed_password: "c9f4e8d3aaa265033c2f517abd5d347bd81d67fa",     created_at: "2013-02-21 04:43:20", updated_at: "2013-02-21 04:43:20", username: "larrydavid",     salt: "d4189f8db685776d3a3c3c4d0700786b7879362d"> 
1.9.3-p362 :034 > me.admin_users_tasks
 => [] 
1.9.3-p362 :035 > task.admin_users_tasks
NoMethodError: undefined method `admin_users_tasks' for #<Task:0x007fca9e159f58>
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-    3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-    3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
    from (irb):35
        from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /Users/larrydavid/.rvm/gems/ruby-1.9.3-p362/gems/railties-    3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3-p362 :036 > 
4

2 回答 2

0

修复了问题。首先,控制台尚未重新启动。

其次在 Rails 3.2 中是什么,

belongs_to :admin_user, :class_name => "AdminUser" :foreign_key => 'admin_user_id'

应该

belongs_to :admin_user, class_name: "AdminUser", foreign_key: 'admin_user_id'
于 2013-02-22T23:42:54.007 回答
0

该代码有点尴尬,主要是因为您使用 HABTM 语法以及提供模型,您应该使用 has_many :through 来完成。我会以不同的方式设置模型,然后导轨可能会不那么混乱。

class Task < ActiveRecord::Base
  has_many :admin_users_tasks
  has_many :admin_users, :through => :admin_users_tasks
end

class AdminUser < ActiveRecord::Base
  has_many :admin_users_tasks
  has_many :tasks, :through => :admin_users_tasks
end

class AdminUserTasks < ActiveRecord::Base
  belongs_to :admin_user
  belongs_to :task
end

HABTM 适用于当您需要多对多连接时,并且您没有连接表的模型,连接的每一侧只有 2 列。没有 ID 或时间戳。

has_many :through 是当你想要一个多对多连接的时候,并且有一个连接表的模型,使它实际上是一个双多一连接,并且连接表有一个 id 列,时间戳,以及两个外部键。

同样从您的代码的外观来看,您对 AdminUser 和 Project 做同样的事情,如果有一个 AdminUserProject 模型加入这两个模型,请使用 has_many :through 而不是 HABTM。

于 2013-02-22T23:30:04.350 回答