help with logic in controller.

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.

    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

atmorell wrote:

     if @message.status = "new"

      if @message.status == "new"

your version is always true!