Tintin81
Tintin81

Reputation: 10207

How to get where clause to return nil?

In my Rails 3 application I am using a before_filter to make sure that the action can only be used by permanent users, i.e. users that are not guests:

def permanent_user
  @user = User.where('guest != ?', true).find(params[:id])
  redirect_to(root_path) unless current_user?(@user)
end

The problem is that I frequently get a ActiveRecord::RecordNotFound error in the first line when the number of total users in the database is very low.

How can my function be improved to either create a @user object or nil?

Thanks for any help.

Upvotes: 0

Views: 83

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

How about this?

def permanent_user
  redirect_to root_path if current_user.guest?
  @user = current_user
end

Upvotes: 4

Related Questions