Tuesday, 27 August 2013

When working with associations in Ruby if the user does not have an event the events method throws and undefined error

When working with associations in Ruby if the user does not have an event
the events method throws and undefined error

I'm building a Rails app where a User has many Events.
If a user has an event and I do current_user.events.each then all is fine
although if the user doesn't have any event an undefined method error is
thrown and the entire applications stops working.
As a PHP user I would first check if the variable was set before doing the
loop. Does Rails have a nifty way of either initiating the method in the
model regardless or should I still check?

No comments:

Post a Comment