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 8789793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:30+00:00 2026-06-13T22:30:30+00:00

I am checking to see if a value in a related table exists quite

  • 0

I am checking to see if a value in a related table exists quite often in my view, in this case, expenses have approvals. If they have not been submitted, then there is no record of them in the approvals table.

I am ending up with some really awkward code

<% if !expense_item.expense_approval || expense_item.expense_approval.approval_status == 0 %>

Is there a way to do an if statement on a value, without having to check if it exists first? Or some default way to set it nil?

  • 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-06-13T22:30:31+00:00Added an answer on June 13, 2026 at 10:30 pm

    You could do the following in your ExpenseItem model:

    delegate :approval_status, :to => :expense_approval, :allow_nil => true
    

    This should allow you to simply do the following:

    if expense_item.approval_status == 0
    

    The try method above certainly works, but I like delegation better because I don’t have to have .try(...) all over my code.

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

Sidebar

Related Questions

This is what i have now. It is checking to see if GoLauncher is
Hi all i have a post value which i am checking to see if
I am a little confused with checking to see if an ofstream file exists
I have an issue where instead of checking to see if there's a duplicate,
Just checking my JS and I have an error, but I cannot see where.
So I have some session checking to see if it's not stolen using PHP.
I have a null check in code that is checking to see whether a
I have a piece of code that is checking to see if a session
I am checking to see if element1, element 2 or element 3 exists and
I was wondering how I would go about checking to see if a table

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.