Why does this output to the console?

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

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

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

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

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

That should work.

Cheers,
Nicholas