SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If Statement help!

    I originally posted this message at another forum, but go no response and it's been a day since I posted it. I thought it was odd because this should be an easy solution:
    -------------
    I need to test critera based on three subjects:

    Code:
    Is the user logged in? If not, skip all code.
    Does the User's "Vote" model contain something other than nil? If so, continue.
    Does the User's Vote correspond to the current page ID? If so, deny another vote. If not, save the vote.
    This would work if I didn't run into one small problem in the controller: I have to check to see if Vote is nil before trying to see if the User's vote corresponds to the page ID. This, in turn, means that I can only use "IF this AND that" for two values- "this" and "that". This is bad, since I have three conditions to check.

    Is there a way to check for all three conditions or not? I also tried nesting the IF statements, but to no avail.

    Perhaps my syntax is wrong?

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If a method or call to an object returns a nil, this is seen as false by Ruby. So if you have a object called "something" and it may either return a nil or return a value - lets say an integer - you can do this:

    Code:
    if something and something < 3 and something > 0
      #carry out process 
    end
    This behaves in the same way as:
    Code:
    unless something.nil?
      if something < 3
        if something > 0
          #carry out process
        end
      end
    end

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I appreciate the help, ReggieB, but I just couldn't get the IF statement syntax to work.

    I found a solution via another means, however.

    Thanks again.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't you think you should post the solution you found for other people who find this thread in the future?
    Bring out our hope and reason, before we pine away.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •