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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:29+00:00 2026-05-27T05:00:29+00:00

I have a form that students are using to rank 6 classes from 1

  • 0

I have a form that students are using to rank 6 classes from 1 to 6. If they select Math as “1” (the hardest), then I don’t want them to be able to select another subject as the hardest. The form will obviously give them the option to select “1” for each subject, but I want to use validations to protect against submission of a form that doesn’t follow instructions

This is a snippet from the form

<div class="field">
    <%= f.label(:math, "Mathp") %>
    <%= f.select:math, 1..6 %> </br>
    </div>

<div class="field">
    <%= f.label(:french, "French") %>
    <%= f.select:french, 1..6 %> </br>
    </div>

I was going to use a validation method like this, but I don’t think the logic of it works (i.e. it doesn’t guard against every possible situation) and it’s probably shitty (non-functional) code too, because i’m just learning how to code (actually I’ve been failing at it for quite a while now). Can you suggest improvements?

validates :rank_favorites 


...

def rank_favorites

unless :math != :french && :french != :history && :history != :spanish && :spanish != :art && :art != :physed return false

end
  • 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-27T05:00:30+00:00Added an answer on May 27, 2026 at 5:00 am

    Your rank_favorites is, sadly, way off but ignorance can be fixed through learning. You’re just comparing a bunch of symbols and that doesn’t do anything useful (at least not as far as you’re concerned), you’re validator reduces to this:

    unless false && false && false && false && false return false
    

    which is equivalent to:

    unless false return false
    

    You probably want to use validate :rank_favorites (not validates) and your validator would add error messages instead of simply return a boolean:

    validate :rank_favorites
    
    #...
    
    def rank_favorites
      ranks = [math, french, history, spanish, art, physed]
      if(ranks.include?(nil))
        errors[:base] << 'Rank all of them you lazy person!'
      elsif(ranks.uniq.length != ranks.length)
        errors[:base] << 'You fail at ranking, no duplicates allowed.'
      end
    end
    

    The Array#uniq method will produce a copy of your array with the duplicates removed, if the lengths don’t match then something was removed and you had duplicate entries.

    You might want to spend some time reading the validations guide:

    Active Record Validations and Callbacks

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

Sidebar

Related Questions

I currently have form that checks if a user has unsubmitted changes when they
I have a table, say STUDENTS, that is related one-to-many to another table, CLASSES.
I have 6 different courses, and I want students to rank them by degree
i have 2 forms. one of them is a form that User select a
I have a list of students that are being added via a form inside
I have form that displays several keywords (standard set of choice lists that changes
I have a form that contains a GridView control which is databound to an
I have a form that I would like to style. specifcally I would like
I have a form that searches all rows in a single table (TServices) that
I have a form that is sending in sizes of things, and I need

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.