# Why does this output to the console?

**URL:** <https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226>\
**Category:** Rails\
**Created:** [July 17, 2009, 1:48am UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226 "2009-07-17T01:48:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 1:48am UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/1 "2009-07-17T01:48:09Z")

</div>

Why would a particular line:

if @channel == nil && current\_associate.watching\_channel\_id != nil &&  
current\_associate.watching\_channel\_id \> 0

alsways output the statement:

false

to the console???

I am using Rails 2.3.2 -Janna B

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 2:57am UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/2 "2009-07-17T02:57:40Z")

</div>

I’m not sure, but you probably should rewrite that:

if @channel.nil? && !current\_associate.watching\_channel\_id.nil? &&  
current\_associate.watching\_channel\_id \> 0

better still why are you checking if watching\_channel\_id is gt 0? Is  
watching\_channel\_id a foreign key for an association for watching  
channel? then you could do this:

if @channel.nil? && current\_associate.watching\_channel

Sorry I couldn’t specifically answer your question, but hope the code  
review was useful.

Cheers,  
Nicholas

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 5:38am UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/3 "2009-07-17T05:38:56Z")

</div>

yes, current\_associate.watching\_channel\_id IS a foreign key to  
channel.id. But I have not specified it as such in the associate model  
or the channel model (not sure how – or if I even need to!) -Janna

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 7:09am UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/4 "2009-07-17T07:09:05Z")

</div>

Janna Brossard wrote:

> yes, current\_associate.watching\_channel\_id IS a foreign key to  
> channel.id. But I have not specified it as such in the associate model  
> or the channel model (not sure how – or if I even need to!) -Janna

if the class name is WatchingChannel, the it will work

belongs\_to :watching\_channel

if the class name of the watching channel is something else, like  
Channel, then it will work

belongs\_to :watching\_channel, :class\_name =\> “Channel”, :foreign\_key =\>  
“watching\_channel\_id”

it’s always false because either @channel is not null, the foreign key  
is not null or the foreign key is zero.

Note that you should never have a foreign key with a zero value

If you make the modification in your model, you can easily do

if @channel.nil? and current\_associate.watching\_channel

and finally

@channel.nil? and @channel == nil is almost the same thing,  
@channel.nil? is easier to read, dough

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/5 "2009-07-17T14:48:39Z")

</div>

so to use your statement, I should put the following in my Associate  
model?

belongs\_to :watching\_channel, :class\_name =\> “Channel”, :foreign\_key  
=\>  
“watching\_channel\_id”

-Janna B

On Jul 17, 1:09 am, Rodrigo D. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 17, 2009, 6:10pm UTC](https://www.ruby-forum.com/t/why-does-this-output-to-the-console/172226/6 "2009-07-17T18:10:14Z")

</div>

That should work.

Cheers,  
Nicholas
