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

  • Home
  • SEARCH
  • 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 4235444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:30:01+00:00 2026-05-21T02:30:01+00:00

Extreme Rails newbie here. I’ve created a simple database consisting of a bunch of

  • 0

Extreme Rails newbie here. I’ve created a simple database consisting of a bunch of questions. Each question has an attribute called challengeNumber (an integer) which identifies which challenge the question belongs to.

My goal is to pass the challengeNumber as a parameter in the URL and return only those questions that match that challenge number.

I tried this code:

<h1>Challenge Test</h1>
<% @challenges.each do |challenge| %>
  <% if challenge.challengeNumber == (params[:challengeNumber]) %>
    <h3><%= challenge.questionText %></h3>
  <% end %>
<% end %>

but it returns nothing when using http://localhost:3000/?challengeNumber=1. However, if I use:

<h1>Challenge Test</h1>
<% @challenges.each do |challenge| %>
  <% if challenge.challengeNumber == Integer(params[:challengeNumber]) %>
    <h3><%= challenge.questionText %></h3>
  <% end %>
<% end %>

I get the desired results (questions with a challengeNumber of 1 returned)

Am I doing this correctly? I want to make sure I am using the right conventions, and I would think a parameter of 1 would be passed as an integer, but it appears to be passed as a string. Is it correct to convert this to an integer as shown in my working code, or am I completely off base here?

Edit to add:

Here’s some detail on my actual setup:

Here is the challenges show.html.erb, to give you an idea of the structure:

<p id="notice"><%= notice %></p>

<p>
  <b>Challengenumber:</b>
  <%= @challenge.challengeNumber %>
</p>

<p>
  <b>Questionnumber:</b>
  <%= @challenge.questionNumber %>
</p>

<p>
  <b>Questiontext:</b>
  <%= @challenge.questionText %>
</p>

<p>
  <b>Answertext:</b>
  <%= @challenge.answerText %>
</p>


<%= link_to 'Edit', edit_challenge_path(@challenge) %> |
<%= link_to 'Back', challenges_path %>

So, this DB could have an entry like this:

challengeNumber: 1, questionNumber: 1, questionText: “Why?”, answerText: “Because”
challengeNumber: 1, questionNumber: 2, questionText: “Who?”, answerText: “Me”

challengeNumber: 2, questionNumber: 1, questionText: “How?”, answerText: “Wish I knew”

(I realize the naming conventions are off per Danne’s reply, will fix once I solve the logic)

So the goal is to send the challengeNumber as a URL parameter (http://localhost:3000/?challengeNumber=1) and have it return both questions associated with that challenge number

Thanks for any help!

  • 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-21T02:30:02+00:00Added an answer on May 21, 2026 at 2:30 am

    There are a few things you should improve to make it more “Rails”.

    First of, it is really unnecessary to load all challenges from the database and then loop through them to see which one is the correct. It will consume more memory then needed. Now i don’t know how your code looks in the controller but I assume that you do something like this:

    @challenges = Challenge.all
    

    That should be changed to

    # Edit: Removed .first so that all matching challenges can be retrieved
    @challenges = Challenge.where(:challengeNumber => params[:challengeNumber])
    

    That way, only the correct challenge will be loaded from the database and you don’t have to care if it is a string or integer. And you can simplify your view to look like this:

    <!-- Edit: Changed to looping though @challenges -->
    <h1>Challenge Test</h1>
    <% @challenges.each do |challenge| %> 
      <h3><%= challenge.questionText %></h3>
    <% end %>
    

    Second, and this is more of a tip to follow the Rails convention. All database fields should be lowercase and separated by underscore so I would recommend to rename challengeNumber to challenge_number

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

Sidebar

Related Questions

Extreme newbie question. I have my database (SQL Server) set up to cascade deletes
Extreme Ruby/Rails novice here: I'm trying to link_to a search action for each individual
Extreme Android developer newbie here...well, new to Android development, not development in general. I
Sorry for the extreme newbie question, but I couldn't find the answer elsewhere and
This is an extreme newbie question. So apologies in advance! I am trying to
Yet another Lucene.net question by an extreme newbie to it. This time, I have
<table> <tbody> <tr style=height:30px> <td>Has a Menu and is goes to the extreme left</td>
Here is part of my code: extern C REGISTRATION_API int extreme(char* lKey) { string
What I am trying to do is create a game that has an extreme
I'm developing a form with PHP and jQuery. Here is the link: http://www.yamaha-motor.com.pe/extreme/php/yamaha/registro/FrmRegistro01.php It

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.