# Failure in comparisons

**URL:** <https://www.ruby-forum.com/t/failure-in-comparisons/98034>\
**Category:** Rails\
**Created:** [July 12, 2006, 3:32am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034 "2006-07-12T03:32:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 3:32am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/1 "2006-07-12T03:32:08Z")

</div>

I’m basically a Ruby noobie.

At the moment I’m having a fair bit of trouble trying to figure out  
what’s going wrong here (partial template):

\<%=  
@project = Project.find(@the\_id)  
if current\_user.userid == @project.client  
hidden\_field ‘messages’, ‘recipient’, ‘value’ =\>  
@project.project\_manager  
else  
hidden\_field ‘messages’, ‘recipient’, ‘value’ =\> @project.client  
end  
%\>

The current\_user.userid method is as follows (from model user.rb):

def userid  
“#{self.id}”  
end

All the values I use there are correct. Say that the current user id is  
2, you are the client (which == 2), and the project manager (id) is 3.  
The form that is output is “hidden\_field ‘messages’, ‘recipient’,  
‘value’ =\> @project.client” instead of “hidden\_field ‘messages’,  
‘recipient’, ‘value’ =\> @project.client”, and it happens every time. Why  
does it not work? I tried stripping white space from the userid, I even  
looked for possible string comparison problems, but there is no  
easy-to-find info online. Please help!

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 3:52am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/2 "2006-07-12T03:52:20Z")

</div>

Hi Isaac,

Isaac Rowntree wrote:

> ```
> hidden_field 'messages', 'recipient', 'value' => @project.client
> 
> ```
> 
> end  
> %\>

For starters, I’d suggest you do the majority of the above, especially  
the  
model access (Project.find) in your controller. Second, even if you  
decide  
not to follow the MVC model, your current\_user will not be available in  
the  
view unless you’re passing it in as a local variable to the partial. If  
not, you’d need to make it an instance variable.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:01am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/3 "2006-07-12T04:01:35Z")

</div>

On 7/12/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if current\_user.userid == @project.client  
> > not to follow the MVC model, your current\_user will not be available in  
> > the  
> > view unless you’re passing it in as a local variable to the partial. If  
> > not, you’d need to make it an instance variable.
> 
> hth,  
> Bill

wouldn’t current\_user be available if it’s a helper method? Like that  
provided by acts\_as\_authenticated.

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:04am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/4 "2006-07-12T04:04:00Z")

</div>

Yes, I should have done it in the controller (slaps wrist). However,  
until I got it working I assumed it didn’t matter where it was - I just  
wanted to figure out why it didn’t work. And no, it still doesn’t work.  
The following code still fails to compare the two numbers together  
correctly.

current\_user.userid returns the value ‘2’  
@project.client returns the value ‘2’  
thus @recipient should equal ‘3’

however, it equals ‘2’ when I output the form (\<%= hidden\_field  
‘messages’, ‘recipient’, ‘value’ =\> @recipient %\>)

def reply\_message  
@project = Project.find(@the\_id)  
if current\_user.userid == @project.client  
@recipient = @project.project\_manager  
else  
@recipient = @project.client  
end  
end

What’s going wrong here???

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:27am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/5 "2006-07-12T04:27:45Z")

</div>

That’s what I thought, and that’s basically what I said in my original  
post. However, I looked everywhere but couldn’t find any indication on  
how to cast to an integer. I know how to cast to a string (to\_s), but  
yeah…

> What is probably happening here is that you are comparing a string “2”  
> to an integer (2).
> 
> “2” == 2 #=\> false

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:20am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/6 "2006-07-12T04:20:05Z")

</div>

On Wednesday, July 12, 2006, at 4:04 AM, Isaac Rowntree wrote:

> however, it equals ‘2’ when I output the form (\<%= hidden\_field
> 
> What’s going wrong here???
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

What is probably happening here is that you are comparing a string “2”  
to an integer (2).

“2” == 2 #=\> false

\_Kevin

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:40am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/7 "2006-07-12T04:40:50Z")

</div>

On Wednesday, July 12, 2006, at 4:27 AM, Isaac Rowntree wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

“2”.to\_i =\> 2

\_Kevin

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 12, 2006, 4:50am UTC](https://www.ruby-forum.com/t/failure-in-comparisons/98034/8 "2006-07-12T04:50:29Z")

</div>

Thanks mate - _sigh_ ruby is too logical sometimes… 😉

> “2”.to\_i =\> 2
