jochen kaechelin wrote:
remail = params[:email]
@user = Request.find(:first, :conditions => ["email = ?", remail])
the error occurs.
What's the prefered way to react when such an error occurs?
Briefly, @user might be nil, so you must guard it with "if @user" each time you use it.
I prefer to put that "if @user" line in only one place, for DRY code. I use a kind of Null Object there:
return @user if @user
who = OpenStruct.new(
:login => '',
:namen => '',
:user_id => nil)
Instead of using "@user" everywhere, I use "someone". If nobody is logged in, "someone" provides only methods that un-logged-in people can use. For example, if logged-in users can delete data, the line "someone.delete_data(a_record)" will delete the data if a user is logged in, and it won't do anything if nobody is logged in.
The heart of Object Oriented programming is replacing if statements with virtual methods that provide the correct behavior.