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 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

I'm not sure exactly where this is happening, but what I'm trying to do
Not sure exactly how to explain this but Imaging you have a table with
I'm not exactly sure if this is the correct question to be asked but
Not sure exactly what I need to do to make this work, so my
I'm not sure exactly how to describe what I want to do, so I'll
I'm not sure exactly how to explain this, so I'll just start with an
I'm not sure exactly how to describe what I want. I want to define
I'm not exactly sure what is happening, but the xml file I am writing
I'm not sure exactly what went wrong here, but ReSharper 4.5 RTM seems to
Note: I am not exactly sure what to name the question, so if someone

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.