# Help with logic in controller

**URL:** <https://www.ruby-forum.com/t/help-with-logic-in-controller/134602>\
**Category:** Rails\
**Created:** [April 12, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/help-with-logic-in-controller/134602 "2008-04-12T18:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 12, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/help-with-logic-in-controller/134602/1 "2008-04-12T18:36:19Z")

</div>

Hello,

This is a method that I have written to show personal messages. If the  
message that is requested is sent “to” the logged in user I will do  
another check and set status to opened if necessary. I can’t figure  
out why the last “else” statement never executes if the first one is  
false: Message.find(params[:id], :conditions =\> {:from )

def show  
if @message = Message.find(params[:id], :conditions =\> {:to =\>  
@user.id }) then  
if @message.status = “new”  
@message.status = “opened”  
@message.save  
end  
else  
@message = Message.find(params[:id], :conditions =\> {:from =\>  
@user.id })  
end  
rescue ActiveRecord::RecordNotFound  
end

Best regards.  
Asbjørn Morell.

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 12, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/help-with-logic-in-controller/134602/2 "2008-04-12T19:44:29Z")

</div>

> ```
> if @message = Message.find(params[:id], :conditions => {:to =>
> 
> ```
> 
> @user.id }) then

cause what you do here is assigning the searchresult to @message  
if @message == Message.find(params[:id],…

i’m not sure right now, if this would work, this would depend on what’s  
in @message. the name implies a string/text, which you can’t compare  
with an active record object anyway

maybe what you want to do is something like:  
if @message == Message.find(params[:id],…).field\_name  
to compare with the data stored in field\_name

or @message contains a record, but then objects are not necessarily  
identical, if they have the same content. maybe you would have to use  
another way to compare them like the eql? function

---

<div class="post-metadata">

**Author:** ![atmorell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@atmorell](https://www.ruby-forum.com/u/atmorell)\
**Post date:** [April 12, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/help-with-logic-in-controller/134602/3 "2008-04-12T20:31:02Z")

</div>

atmorell wrote:

> ```
> if @message.status = "new"
> 
> ```

```
  if @message.status == "new"

```

your version is always true!
