7stud2
(7stud --)
1
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?
···
--
Posted via http://www.ruby-forum.com/.
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 
···
On Wed, Apr 25, 2012 at 4:13 PM, Christopher D. <lists@ruby-forum.com> 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?
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
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 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?
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 Wed, Apr 25, 2012 at 8:38 PM, Ryan Davis <ryand-ruby@zenspider.com>wrote:
2) You do know that both 0 and 1 are truthy in ruby, yes? Don't get bit by
that.
--
-yossef
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
···
On Thu, Apr 26, 2012 at 1:46 AM, Hassan Schroeder <hassan.schroeder@gmail.com> wrote:
On Wed, Apr 25, 2012 at 4:13 PM, Christopher D. <lists@ruby-forum.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
7stud2
(7stud --)
6
Yossef Mendelssohn wrote in post #1058396:
···
On Wed, Apr 25, 2012 at 8:38 PM, Ryan Davis > <ryand-ruby@zenspider.com>wrote:
2) 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!
--
Posted via http://www.ruby-forum.com/\.
Wouldn't this have to be
@user = (User.find_by_username("Tom")) ? @user.id : 0
?
···
On Thu, Apr 26, 2012 at 3:11 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Thu, Apr 26, 2012 at 1:46 AM, Hassan Schroeder > <hassan.schroeder@gmail.com> wrote:
@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
···
On Fri, Apr 27, 2012 at 1:35 AM, Eric Christopherson <echristopherson@gmail.com> wrote:
On Thu, Apr 26, 2012 at 3:11 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:
On Thu, Apr 26, 2012 at 1:46 AM, Hassan Schroeder >> <hassan.schroeder@gmail.com> 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
?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/