ruby on rails - display the field even though it is empty for some objects -


i'm trying display school name users. when sign users can select school belong (not though column left blank).

at moment have:

if user.is_a? student   %td= user.school end 

which finds schools students belong to, displays activerecord record. each of schools has name , if select single student , do: @student.school.name gives me name of school no issues.

however if do:

if user.is_a? student   %td= user.school.name end 

it keeps throwing undefined methodname' nil:nilclass`

i pretty sure because of students don't have school. have tried add if statement can't seem make work. tried variations of:

if user.is_a? student && !user.school.blank? 

with .any?, .empty? none of them work, no method .blank? etc.

any please?

i 1 of these things

a) quickest

 %td= user.school && user.school.name 

b) cleanest (or, @ least, cleaner)

#in user class def school_name   self.school && self.school.name end  #in view %td= user.school_name 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -