将站点从 Rails 2.2.2 迁移到 Rails 3
我正在运行一个查询并将其存储在变量@user 中,但是当我尝试访问@user 的成员时,我得到一个错误,即使@user 已经水合并且具有“is_admin”的值
### @user.inspect returns
[#<B2bUser id: 398, name: "Tim Test", email: "tim_test@test.com", username: "timtest", password: "foo", is_admin: true, company: "Test Co.", sent_welcome_email: true, created_at: "2011-12-14 08:32:31", send_email_reminders: false, api_username: "api.tim_test@test.com", api_password: "FOOBAR", login_attempts: nil, last_login_attempt_at: nil>]
但是当我从它的角度调用它时:
<% if (@user.is_admin) %>
我明白了:
Completed 500 Internal Server Error in 15ms
ActionView::Template::Error (undefined method `is_admin' for #<ActiveRecord::Relation:0xaa5db4c>):
14:
15: <%= render :partial => "/warning" %>
16:
17: <% if (@user.is_admin) %>
18:
19:
20:
在同一视图中,当返回多于 1 行时会发生相同类型的错误(未定义的方法 p.vendor_name),例如:
<% for p in @privileges %>
<% next if !p.can_setup_purchase_orders and !p.can_setup_instant_electronic_delivery %>
<h2>For <%= p.vendor_name %></h2>
这是我的控制器
class HomeController < ApplicationController
before_filter :authenticate
def index
@nav = []
@user = B2bUser.where(:id => request.session[:user_id])
#logger.debug @user.inspect
@privileges = B2bPrivilege.lookup_all request.session[:user_id]
#logger.debug @privileges.inspect
@can_asn = false
@can_push_xml = false
for p in @privileges
if p.can_setup_advance_ship_notification?
@can_asn = true and break
end
if p.can_setup_xml_pushes?
@can_push_xml = true and break
end
end
end
end
这是我的方法lookup_all class B2bPrivilege < ActiveRecord::Base
def B2bPrivilege.lookup_all current_user_id
#return B2bPrivilege.find(:all, :conditions => "b2b_user_id = #{current_user_id}", :joins => "LEFT JOIN vendors ON vendors.id = b2b_privileges.vendor_id", :select => "b2b_privileges.*, vendors.name AS vendor_name")
return B2bPrivilege.joins('LEFT JOIN vendors ON vendors.id = b2b_privileges.vendor_id').where('b2b_user_id' => current_user_id)
#return B2bPrivilege.find_by_sql["SELECT b2b_privileges.*, vendors.name AS vendor_name FROM b2b_privileges LEFT JOIN vendors ON vendors.id = b2b_privileges.vendor_id WHERE b2b_user_id = ?", current_user_id]
end
我需要以不同的方式访问@user 中的成员吗?这曾经在 Rails 2.2.2/Ruby 1.8.7 中工作
谢谢