开发者

Turning an ActiveRecord::Relation into a model

very beginner question. I am using Rails 3's query interface as shown:

class User < ActiveRecord::Base

def self.authenticate
 if Rails.env = 'development'
   self.where('username = ?', 'development_user')
 else
  开发者_开发技巧 self.where('username = ?', request.env['REMOTE_USER'])
 end
end

end

This is returning an ActiveRecord::Relation object, where in reality I want the User object that relates to the query. How do I turn this into a User object?


You need to "commit" the query with all, first, or find.

def self.authenticate
 user = Rails.env.development? ? "development_user" : request.env['REMOTE_USER']
 self.where('username = ?', user).first
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜