Method Missing as Filter

Let’s say I have a model called User. User has two attributes or table
columns: id, username

By using the code below I can check that there is a username “Tom” and
return 1 ifthere is and 0 if there is not

User.find_by_username(“Tom”)? 1 : 0

How would I find out Tom’s corresponding id?

For example I’m looking for something along the lines of:
User.find_by_username(“Tom”).id

On Wed, Apr 25, 2012 at 4:13 PM, Christopher D. [email protected]
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

I’ll bet there will be other suggestions :slight_smile:

On Apr 25, 2012, at 16:13 , Christopher D. wrote:

Let’s say I have a model called User. User has two attributes or table
columns: id, username

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?

  1. This is a rails question, not a ruby question. Please use the
    appropriate forum.

  2. You do know that both 0 and 1 are truthy in ruby, yes? Don’t get bit
    by that.

On Wed, Apr 25, 2012 at 8:38 PM, Ryan D.
[email protected]wrote:

  1. You do know that both 0 and 1 are truthy in ruby, yes? Don’t get bit by
    that.

And if you’re having trouble telling whether something is truthy, let me
offer GitHub - ymendel/truthy: Easily find out the truthiness of any Ruby object. .

On Thu, Apr 26, 2012 at 1:46 AM, Hassan S.
[email protected] wrote:

@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 :slight_smile:

You won.

Cheers

robert

On Thu, Apr 26, 2012 at 3:11 AM, Robert K.
[email protected] wrote:

On Thu, Apr 26, 2012 at 1:46 AM, Hassan S.
[email protected] wrote:

@user = User.find_by_username(“Tom”) ? @user.id : 0

Wouldn’t this have to be
@user = (User.find_by_username(“Tom”)) ? @user.id : 0
?

Yossef M. wrote in post #1058396:

On Wed, Apr 25, 2012 at 8:38 PM, Ryan D.
[email protected]wrote:

  1. You do know that both 0 and 1 are truthy in ruby, yes? Don’t get bit by
    that.

And if you’re having trouble telling whether something is truthy, let me
offer GitHub - ymendel/truthy: Easily find out the truthiness of any Ruby object. .

Thanks for the help guys…

“really appreciate it”.truthy? # => true

hahaha!

On Fri, Apr 27, 2012 at 1:35 AM, Eric C.
[email protected] wrote:

On Thu, Apr 26, 2012 at 3:11 AM, Robert K.
[email protected] wrote:

On Thu, Apr 26, 2012 at 1:46 AM, Hassan S.
[email protected] wrote:

@user = User.find_by_username(“Tom”) ? @user.id : 0

Wouldn’t this have to be
@user = (User.find_by_username(“Tom”)) ? @user.id : 0
?

Why should it? Those brackets do not make a difference because that
is how it’s parsed anyway.

Kind regards

robert