ruby on rails - How to add exception handling to my before_action -



ruby on rails - How to add exception handling to my before_action -

i have before_action method this:

def current_user @current_user ||= user.find(:id => session[:id]) end

and phone call method this:

def get_food user nutrient = food.find(:id => user.id) end

this fine, want add together exception handling.

when user nil want utilize @current_user:

def get_food user nutrient = food.find(if user nil want utilize @current_user.id) end

of course, can write this:

def get_food user if user.nil? nutrient = food.find(@current_user.id) else nutrient = food.find(user.id) end

or, best way?

def get_food user nutrient = food.find(user == nil? @current_user.id : user.id) end

i'm curious there improve way adding simple if statement within param?

the shortest 1 lines can think of this:

food.find((user || current_user).id) food.find(user.try(:id) || current_user.id) food.find(user ? user.id : current_user.id)

not sure if impovement in readability. prefer this:

def get_food(user) user ||= current_user food.find(user.id) end

ruby-on-rails ruby

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -