Finds in Rails 3 and ActiveRelation

Posted by TheDelChop on Stack Overflow See other posts from Stack Overflow or by TheDelChop
Published on 2010-05-28T15:34:45Z Indexed on 2010/05/29 3:32 UTC
Read the original article Hit count: 303

Guys,

I'm trying to understand the new arel engine in Rails 3 and I've got a question.

I've got two models, User and Task

class User < ActiveRecord::Base
  has_many :tasks
end

class Task < ActiveRecord::Base
  belongs_to :user
end

here is my routes to imply the relation:

resources :users do
  resources :tasks
end

and here is my Tasks controller:

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

Problem is, I get the following error when I try to invoke the new action:

NoMethodError: undefined method `tasks' for #<ActiveRecord::Relation:0x3dc2488>

I am sure my problem is with the new arel engine, does anybody understand what I'm doing wrong?

Sorry guys, here is my schema.db file:

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

Thank you,

Joe

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails3