新手问题 ActiveRecord 读取记录问题...

yu3h40 · 2015年01月14日 · 最后由 sensi 回复于 2015年03月11日 · 2546 次阅读

该算个特别基础的问题了... Google 了好久也没找到应该是没人像我这么笨

current = User.find(id)

取得 current 之后想要读取这个记录中 username 的内容.. 应该怎么做 尝试用 current.username 报错 之前代码少的时候是这样

current.each do |current_user|
    id  = current_user.id
    session[:id] = id
end

代码一多就特别繁琐,这里应该是有更简单的方法..但是我没找到
thanks!

lz 可以把 current.username 时报错的 log 发一下

#1 楼 @heylonj

NoMethodError - undefined method `username' for #<User::ActiveRecord_Relation:0x000000037468c0>:
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:136:in `method_missing'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb:99:in `method_missing'
    game.rb:77:in `block in <class:Game>'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `block in compile!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `[]'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (3 levels) in route!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:985:in `route_eval'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (2 levels) in route!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1006:in `block in process_route'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `catch'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1004:in `process_route'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:964:in `block in route!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `each'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:963:in `route!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1076:in `block in dispatch!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1073:in `dispatch!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `block in call!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `block in invoke'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `catch'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1058:in `invoke'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:898:in `call!'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:886:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/xss_header.rb:18:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/path_traversal.rb:16:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/json_csrf.rb:18:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-protection-1.5.0/lib/rack/protection/frame_options.rb:31:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0/lib/rack/session/abstract/id.rb:225:in `context'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0/lib/rack/session/abstract/id.rb:220:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0/lib/rack/nulllogger.rb:9:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/rack-1.6.0/lib/rack/head.rb:13:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:21:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:180:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:2014:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `block in call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1788:in `synchronize'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `block in spawn_threadpool'
Unexpected error while processing request: undefined method `join' for #<String:0x007fd98855e538>
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:37:in `rescue in call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/show_exceptions.rb:21:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:180:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:2014:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `block in call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1788:in `synchronize'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/sinatra-1.4.5/lib/sinatra/base.rb:1478:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `call'
    /home/yushao/.rvm/gems/ruby-2.1.5/gems/eventmachine-1.0.3/lib/eventmachine.rb:1037:in `block in spawn_threadpool'


ActiveRecord_Relation

这还是一个 relation 不是 User 的 instance .first 试试?

#3 楼 @ripple 解决了 thx!

#4 楼 @yu3h40 请教一个问题。current 是一个实例变量,each 方法难道不是遍历数组和哈希的方法吗,这里怎么行得通的?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册