ruby on rails - Only calling a method if a hash is populated -


i have instance variable called

@filtered_ratings = params[:ratings].keys  

however, if params[:ratings] nil , #keys called on there error raised. if @filtered_ratings nil, want @filtered_ratings set empty array (or hash). there easy way of doing without code? thanks!

@filtered_ratings = params[:ratings].keys if params[:ratings].respond_to? :keys 

edit:

 # if params[:ratings] nil return []  @filtered_ratings = params[:ratings].try(:keys) || [] 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -