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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:56:27+00:00 2026-05-26T20:56:27+00:00

which way below is more efficient to accomplish this task. I want to loop

  • 0

which way below is more efficient to accomplish this task. I want to loop through a model, check if a list made up of integer vals associated with each model_id is greater than 0. If it is then take the corresponding models into a list of models.

@models = Model.find(:all).collect{|m| m }.reject{ |i| modellist[i.id] < 1 }

or like this

finalModels = []
Model.find_each do |model|
  if modellist[model.id] > 0 #edited
  #if modellist[model.id] != 0
    finalModels.push( model )
  end
end
@models = finalModels

Im leaning towards the second approach, but im not sure. Maybe some insight into how .collect and .reject works to see how efficient it is.

  • Edit

My model is called Picture. modellist (or pList) contains data similar to this.

[nil,nil,nil,3,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,
nil,nil,7,nil,nil,nil,0,nil,nil,nil,0,0,nil,nil,1,3]

I the index number of pList corresponds to the picture id for this. So i need to find pics where pList[picture id] is greater than 0.

  • Edit

Used Benoit Garrets answer. What i had to do was make sure that pList was declared by pList = Hash.new and not pList = []. the exact query i used was

@pictures = Picture.find(pList.select {|k, v| v > 0}.keys)
  • 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-26T20:56:28+00:00Added an answer on May 26, 2026 at 8:56 pm

    Assuming your modellist and pList already exist and are Hash, you could filter them before and use find with an array:

    @models = Model.find(modellist.reject {|k, v| v < 1} .keys)
    

    Same thing with your second example:

    @pictures = Picture.find(pList.reject {|k, v| v > 0} .keys)
    

    This way, you won’t be looping over your entire database.

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

Sidebar

Related Questions

Which one of the two alternatives below is more efficient? Any recommendations to further
I have a list M below which came from an external file (This is
Which way is better for removing float decimals places or is there a more
Maybe this way it will make more sense. MySQL table Code: id1 | id2
I have this code below... if (x == 0) { $(#scenariotitle).val(''); $(#scenariosuffix).val(''); } ...which
I have a list of floating point data in which I want to find
i written code using for loop in 2 ways like below first way for(int
I have python code below that will loop through a table and print out
Which way do you prefer to create your forms in MVC? <% Html.Form() {
In which way could I call the function that automatically opens my annotation (with

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.