Problem with case / when conditional of db values


#1

Hello *,

I have been trying to perform conditional actions in the controller
depending on values in the db - in this case, the object ‘info’ and the
column ‘status’. I would just like the controller to run an action
depending on the value in the column, in this case ‘OPEN’ or ‘SENT’:

  1. def infoupdate
  2. case @info.attribute(:status)
  3. when ‘OPEN’

    when ‘SENT’

but keep getting:

‘You have a nil object when you didn’t expect it!
The error occured while evaluating nil.attribute’

I believe something is wrong with my code in line 2. and I have been
trying many alternatives. Please, let me know what I am doing wrong.

Thank you,

Alex


#2

Hi Alex,

is the @info instance variable guarenteed to be set before the
controller
gets to the infoupdate method?

Perhaps if you set @info in a before_filter to ensure that it is
available
at all times.

Alternatively you could make this an instance method of your model

eg
@info.info_update

def info_update
case self.status


end