Forum: Ruby on Rails Problem with case / when conditional of db values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex S. (Guest)
on 2006-05-04 10:05
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
Daniel -. (Guest)
on 2006-05-04 10:15
(Received via mailing list)
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
This topic is locked and can not be replied to.