2010-05-28 49 views
1

我想了解Rails 3的新AREL引擎和我有一個問題。發現用Rails 3和ActiveRelation

我有兩個型號,用戶和任務

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 
+0

你有沒有在你的任務定義user_id列?你能否將你的遷移文件添加到你的問題中。 – 2010-05-28 15:39:18

回答

2

我相信你想:

def load_user 
    @user = User.where(:id => params[:user_id]).first 
end 

直到你問一個記錄它會留關係。


find(params[:user_id])仍然可以工作並返回記錄。

+0

這工作,這是真棒,我想我不太會了解這個新界面到底是什麼呢。謝謝。 – TheDelChop 2010-05-28 15:54:29

1

請問,如果你改變你的load_user方法如下工作?

def load_user 
    @user = User.find(params[:user_id]) 
end 

另外,我覺得你可能需要改變你的new行動:

def new 
    @task = @user.tasks.build 
end 
+0

嗯,是的,沒有,發現將在rails 3.1中被棄用,所以我寧願不使用我知道即將被棄用的東西。爲什麼要構建而不是新的? – TheDelChop 2010-05-28 15:49:09

+0

你從哪裏得到'find'將被棄用的想法?它通過一個選項散列來發現將被棄用:http://m.onkey.org/2010/1/22/active-record-query-interface – 2010-05-28 16:04:46

0

這其實很簡單。這裏有一種方法...

def new 
    @task = @user.tasks.new 
    end 

    private 

    def load_user 
    # you must call a non-deferred operator to actually return 
    # the tuple that is connected to your tasks 
    @user = User.where(:id => params[:user_id]).first 
    end 

一定要看看我正在做的關於Active Relationship的七部分學習系列。第一集將幫助你理解你的錯誤更清楚。 http://Innovative-Studios.com/#pilot

find()方法如前所述是不是在某些情況下,不建議使用。我會限制find()用於原子值(您正在搜索特定項目/索引的地方)。任何可能基於集合的東西我都會堅持ActiveRecord for Arel中的Where(限制)子句包裝。