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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:37:13+00:00 2026-06-01T12:37:13+00:00

After much googling and console testing I need some help with arrays in rails.

  • 0

After much googling and console testing I need some help with arrays in rails. In a method I do a search in the db for all rows matching a certain requirement and put them in a variable. Next I want to call each on that array and loop through it. My problem is that sometimes only one row is matched in the initial search and .each causes a nomethoderror.

I called class on both situations, where there are multiple rows and only one row. When there are multiple rows the variable I dump them into is of the class array. If there is only one row, it is the class of the model.

How can I have an each loop that won’t break when there’s only one instance of an object in my search? I could hack something together with lots of conditional code, but I feel like I’m not seeing something really simple here.

Thanks!

Requested Code Below

@user = User.new(params[:user])  
if @user.save      
  #scan the invites dbtable and if user email is present, add the new uid to the table
    @talentInvites = TalentInvitation.find_by_email(@user.email)
    unless @talentInvites.nil?
      @talentInvites.each do |tiv|
        tiv.update_attribute(:user_id, @user.id)
      end  
    end
....more code...
  • 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-01T12:37:15+00:00Added an answer on June 1, 2026 at 12:37 pm

    Use find_all_by_email, it will always return an array, even empty.

    @user = User.new(params[:user])  
    if @user.save      
      #scan the invites dbtable and if user email is present, add the new uid to the table
        @talentInvites = TalentInvitation.find_all_by_email(@user.email)
        unless @talentInvites.empty?
          @talentInvites.each do |tiv|
            tiv.update_attribute(:user_id, @user.id)
          end  
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After much googling I haven't been able to find a way to add additional
How do I make this into a grayscale? After much googling it seems the
After much googling I have been wondering what the benefits/differences are between mysql and
After much googling, stackoverflowing (?) and blog-reading I still cant completely solve the issue.
This is probably jquery basics, but I can't find a solution after much googling.
I want to start using table relations in a new project. After some googling
After much research and trial and error I found how to store the items
After much fiddling, I've managed to install the right ODBC driver and have successfully
So after much debate back and forth, I've come up with what I think
I know about $(Delphi) and $EDNAME but after much seaching I cant find a

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.