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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:49:32+00:00 2026-06-02T05:49:32+00:00

I have an invoice model with approver_note, po_number and state_id. I need validations to

  • 0

I have an invoice model with approver_note, po_number and state_id.

I need validations to check:

validates :approver_note, :presence => true, {:scope => state_id == 3}
validates :po_number, :presence => true, {:scope => state_id ==2}

So, if the user selects state_id = 3, he must enter a note.
If he selects state_id = 2, he must enter a po_number.

Any assistance would be great… thanks!

  • 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-02T05:49:33+00:00Added an answer on June 2, 2026 at 5:49 am

    You’re looking for the :if option instead of :scope.

    validates :approver_note, :presence => true,
      :if => lambda { |invoice| invoice.state_id == 3 }
    

    But since a lambda is a little ugly, I’d probably add a method to encapsulate what you’re doing a bit better:

    validates :approver_note, :presence => true, :if => :requires_note?
    validates :po_number, :presence => true, :if => requires_po_number?
    
    def requires_note?
      state_id == 3
    end
    
    def requires_po_number?
      state_id == 2
    end
    

    If you actually have a bunch of different attributes that are required when state_id is 3, not just a note, then you may want something like this:

    validates :approver_note, :presence => true, :if => :green_state?
    validates :po_number, :presence => true, :if => orange_state?
    
    def green_state?
      state_id == 3
    end
    
    def orange_state?
      state_id == 2
    end
    

    (Replace “green” with — I dunno — “high_documentation” or whatever makes sense in your world.)

    Or maybe you want to let the state decide what it is:

    def green_state?
      state.green?
    end
    

    It really does help to make the terminology in your code adhere more closely to your real-world language, as opposed to “3” and “2”.

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

Sidebar

Related Questions

Let's say I have a User model, and an Invoice model with a belongs_to
I have an Invoice model in a Rails app. The Invoice has_many Products and
Let model Quote have attributes [price, description] Let model Invoice have attributes [price, description,
I have a model Invoice with a method def item_objects(self): return self.invoiceitem_set.filter(kind='item') I then
I have a FinancialDocument#document_type model attribute. I'd like to let the user select the
I have an invoice model, that has many invoice items. I have a form
I have a model Invoice with attribute payment_status. payment_status has fixed values unpayed|partial_payed|payed that
I have an Invoice model with a boolean field: is_overdue . This field is
I have an Invoice model which belongs to Customer (and of course Customer has_many
I have the following that will enumerate over the model and add an invoice

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.