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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:14+00:00 2026-05-28T06:42:14+00:00

I have something like <% @users.each do |u| %> … <% @interests.each do |i|

  • 0

I have something like

<% @users.each do |u| %>
  ...
  <% @interests.each do |i| %>
    <% if i joins u in interests_users %> <-- That is not working that way! = Problem
      ...
      <% i.id %>
      ...
    <% end %>
  <% end %>
  ...
<% end %>

I need to output each interest-id that joins users in interests_users for every user. But i cannot use a sql query for each user, because that would be too much for the server, so i query the whole interests table and want to filter it in the view.
Anyone got a simple solution for that?

  • 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-28T06:42:15+00:00Added an answer on May 28, 2026 at 6:42 am

    You’re thinking about this too view-centric – this is a data issue, so the model(s) should take care of it.

    I’m guessing you have an n-m relationship between users and interests, with the interests-users join table in the middle. The models should define that relationship something like this:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :interests
    end
    
    class Interest ...
      has_and_belongs_to_many :users
    end
    

    This is with has_and_belongs_to_many (HABTM). The models then already take care of “selecting” which interests are “on” each user, simply query them with user_instance.interests, i.e. do

    <% @users.each do |u| %>
      ...
      <% u.interests.each do |i| %>
    

    If that generates a query for interests for each user, you can eager load the data when getting the users:

    @users = User.includes(:interests).all
    

    Edit:

    Oh yeah and if you want to list all interests and mark those a user has associated, something like this should work:

    @interests.each do |interest|
      if u.interests.include?(interest) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have something like a auction and for each deal or that auction
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
I have a MySQL database that looks like this: users ( id , name
I'm working on a database that needs to represent computers and their users. Each
I have a rails app I am working on that allows users to create
Feel like this should be something easy that I'm missing. I have a table
If i have a loop such as users.each do |u| #some code end Where
How do I make something like user-defined __repr__ in Python? Let's say I have
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <node TEXT= txt A /> <node TEXT= txt X

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.