我正在尝试了解 Rails 3 中的新 arel 引擎,但我有一个问题。
我有两个模型,用户和任务
class User < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :user
end
这是我暗示这种关系的路线:
resources :users do
resources :tasks
end
这是我的任务控制器:
class TasksController < ApplicationController
before_filter :load_user
def new
@task = @user.tasks.new
end
private
def load_user
@user = User.where(:id => params[:user_id])
end
end
问题是,当我尝试调用新操作时出现以下错误:
NoMethodError: undefined method `tasks' for #<ActiveRecord::Relation:0x3dc2488>
我确定我的问题出在新的 arel 引擎上,有人知道我做错了什么吗?
对不起,这是我的 schema.db 文件:
ActiveRecord::Schema.define(:version => 20100525021007) do
create_table "tasks", :force => true do |t|
t.string "name"
t.integer "estimated_time"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
add_index "users", ["username"], :name => "index_users_on_username", :unique => true
end