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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:39:31+00:00 2026-05-12T19:39:31+00:00

Not exactly sure how to look this up, but I’m not finding the solution

  • 0

Not exactly sure how to look this up, but I’m not finding the solution anywhere. I want to assign an array of users an array of websites. Like so:

users = User.all
sites = Site.all

users.each do |user|
  sites.each do |site|
     user.websites << site
  end
end

Obviously, this does not work because I’m missing something about block scope. However, this works:

users.each do |user|
  Site.all.each do |site|
    user.websites << site
  end
end

But I’m trying to eliminate the extra calls to the DB (Site.all.each …) and learn something about Ruby in the process. Any help is greatly appreciated.

  • 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-12T19:39:31+00:00Added an answer on May 12, 2026 at 7:39 pm

    If your User model is in a has many relationship with Websites (classname Sites), then you can just do

    users = User.all
    sites = Site.all
    
    users.each do |user|
      user.websites = sites
    end
    

    Edit: For Harpastum:

    If user.websites contains items not in Site.all that you don’t want over written the following works.

    users = User.all
    sites = Site.all
    
    users.each do |user|
      user.websites << sites
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure exactly how to explain this but Imaging you have a table with
I'm not exactly sure how to ask this question really, and I'm no where
Not sure exactly how to word this question ... so edits are welcomed! Anyway
Not sure what exactly is going on here, but seems like in .NET 1.1
This is not exactly a programming question, but it's highly related. We are writing
I'm not sure exactly what went wrong here, but ReSharper 4.5 RTM seems to
Coming from a desktop background I'm not sure exactly how to pass the exceptions
My question is not exactly the same as this one (it's not theoretical, there
I have a few tables that have similar fields but not exactly the same.
After having a search around I could not find exactly what I want, I

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.