Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 330
Nick Barrett Avatar asked Feb 03 '11 04:02

Nick Barrett


1 Answers

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
like image 88
Chris Heald Avatar answered Oct 21 '22 02:10

Chris Heald