开发者

Rails 3 - How to Not to Error if no record is found

In my permissions controller, I use Active record to 开发者_Go百科look for a permission:

@permission = Permission.find(params[:user_id])

If this returns a result I then look up the permission.name And pass this to my controller.

Problem is sometimes this does returna result, othertimes it does not. When it does not, it errors. How can I prevent that from occuring?

Use Case: 1. If the user does have a permission record, show it and let the user change it 2. If not, show they don't have a permission record and allow the user to set a permission.

Thanks


@permission = Permission.find_by_id params[:user_id]

The idea is that if you are using the "first param is the id" version of find, you know exactly what you are looking for, and if it isn't there, thats a problem. If you use one of the more generic finder syntaxes (like find_by_field_name), the assumption is that if it isn't there, that is an acceptable situation so just return nil.


I know this is old, but I just found this and I want to suggest a different way of handling this situation. ActiveRecord::RecordNotFound is nothing to be afraid of. A user may pass in a valid record id, but that record may not belong to them (i.e. if you do something like current_user.widgets.find(params[:id])). I prefer to handle it like this:

def show
  begin
    @permission = Permission.find(params[:user_id])
  rescue ActiveRecord::RecordNotFound
    # however you want to respond to it
  end
end


ActiveRecord#find with an int parameter is a targeted find. Rails raises RecordNotFound if the record isn't found.

This is different from using find with parameters like :first or :all, which is more of a search; Rails returns nil for no records in those cases. If you want to avoid the raising of an exception, use one of those options or the corresponding method names.

Example:

@permission = Permission.find(:first, :id => params[:id])


The other way:

@permission = Permission.find_all_by_id params[:user_id]

I think that it useful if user_id is a array


I think this will work, I haven't tested it.

if @permission
  # Handle when the permission exists
else
  # Handle when the permission doesn't exist
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜