Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:17:25+00:00 2026-05-17T01:17:25+00:00

I have the following if statement: if !projectid_viewing.nil? && !user.role(projectid_viewing).nil? && user.role(projectid_viewing) == ‘admin’

  • 0

I have the following if statement:

if !projectid_viewing.nil? && !user.role(projectid_viewing).nil? && user.role(projectid_viewing) == 'admin'

What I’m trying to do with the above, is not have the if break is projectid_viewing or user.role are nil. projectid_viewing seems to work great but user.role keeps breaking, giving the following error:

undefined method `role' for nil:NilClass

Can you help me with the if statement and is there a more elegant way to write the statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:17:26+00:00Added an answer on May 17, 2026 at 1:17 am

    You need to make sure that user is not nil as well.

    Note that in Ruby, a value of nil in a conditional statement will be interpreted as false. Thus, if you use a variable with a nil value in a conditional statement, it will also evaluate to false. Knowing this, you can simplify statements like !projectid_viewing.nil? to just the name of the variable, and it will work just the same.

    if projectid_viewing && user && user.role(projectid_viewing) == 'admin'
    

    The above is just plain Ruby, but you said you’re using Rails 3. Rails has a neat little method: Object#try. It would allow you to simplify this statement further to the following:

    if projectid_viewing && user.try(:role, projectid_viewing) == 'admin'
    

    The Object#try method will protect you if the value of user is nil, and that entire second part of the expression will return nil (i.e., false).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following statement SELECT id, descr from mytable which returns 1, 'Test1'
I have the following SQL Statement. I need to select the latest record for
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have the following code, this code was recommended to me by stackoverflow user
I have the following code in a custom user control that contains a DropDownList
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
When using IF statements in Python, you have to do the following to make
I have following string String str = replace :) :) with some other string;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.