ruby - Using Rails find_or_initialize_by_email, how to determine if record if found or initialized? -


in 1 of controllers, i'm doing:

user = user.find_or_initialize_by_email(@omniauth['info']['email']) 

i need know if records found or initialized. had tried this:

if user else end  

but not work there user. right way know if find or initialize found or initialized?

thanks

you should able use persisted?:

if user.persisted? 

edgeguides.rubyonrails.org says following find_or_initialize_by:

the find_or_initialize_by method work find_or_create_by call new instead of create. means new model instance created in memory won't saved database.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -