From: Robert Klemme Date: 2012-04-26T17:11:23+09:00 Subject: Re: Method Missing as Filter On Thu, Apr 26, 2012 at 1:46 AM, Hassan Schroeder wrote: > On Wed, Apr 25, 2012 at 4:13 PM, Christopher D. wrote: > >> I can check that there is indeed a username ("Tom") and return 1 if >> there is one and 0 by using this code >> User.find_by_username("Tom")? 1 : 0 >> >> How would I find out Tom's corresponding id? > > Here's a couple of ways: > >  @user = User.find_by_username("Tom") ? @user.id : 0 > >  User.find_by_username("Tom").id rescue 0 Why waste a member variable for this? I'd also stick with the truthy semantics: def find_user_id_by_name(name) user = User.find_by_username(name) and user.id end > I'll bet there will be other suggestions :-) You won. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/