我正在关注为 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 >