English 中文(简体)
Finds in Rails 3 and ActiveRelation
原标题:

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
最佳回答

I believe you want:

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

Until you ask for a record it will stay a relation.


But find(params[:user_id]) will still work and return the record.

问题回答

Does it work if you change your load_user method as shown below?

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

Also, I think you might need to change your new action to:

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

Don t confuse the Arel gem s interface with the new ActiveRecord query interface. The syntax described here will not work: http://github.com/brynary/arel

ActiveRecord uses Arel under the hood but creates its own Arel-like API. Here s a brief look at the new ActiveRecord query interface: http://m.onkey.org/2010/1/22/active-record-query-interface

It s actually pretty simple. Here is one method...

  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

Be sure to take a look at the seven part learning series I am doing on Active Relation. The first episode will help you understand what your error was more clearly. http://Innovative-Studios.com/#pilot

find() IS NOT deprecated in some instances as stated before. I would limit the use of find() for atomic values (where you are searching for a specific item/index) Anything that could possibly be collection based I would stick to the Where (restriction) clause wrapper in ActiveRecord for Arel.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签