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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:38:46+00:00 2026-06-10T19:38:46+00:00

I have a quite complicated relation between models and are now frustrated by a

  • 0

I have a quite complicated relation between models and are now frustrated by a SQL Query to retrieve some objects.

given a Product model connected to a category model via a has_many :through association and a joint table categorization.
Also a User model connected to this category model via a has_many :through association and a joint table *category_friendship*.

I am now facing the problem to retrieve all products, which are within the categories of the array user.category_ids. However, I can’t just not manage to write the WHERE statement properly.

I tried this:

u = User.first
uc = u.category_ids
Product.where("category_id IN (?)", uc)

However this won’t work, as it doesn’t have a category_id in the product table directly. But how can I change this to use the joint table categorizations?

I’m giving you the model details, maybe you find it helpful for answering my question:

Product.rb

class Product < ActiveRecord::Base

 belongs_to :category

 def self.from_users_or_categories_followed_by(user)
 cf = user.category_ids
 uf = user.friend_ids

 where("user_id IN (?)", uf) # Products out of friend_ids (uf) works fine, but how to extend to categories (cf) with an OR clause?
 end

Category.rb

class Category < ActiveRecord::Base
 has_many :categorizations
 has_many :products, through: :categorizations
 has_many :category_friendships
 has_many :users, through: :category_friendships

Categorization.rb

class Categorization < ActiveRecord::Base

 belongs_to :category
 belongs_to :product

Category_friendship.rb

class CategoryFriendship < ActiveRecord::Base

 belongs_to :user
 belongs_to :category

User.rb

class User < ActiveRecord::Base

has_many :category_friendships
has_many :categories, through: :category_friendships

def feed
 Product.from_users_or_categories_followed_by(self) #this should aggregate the Products
end

If you need more details to answer, please feel free to ask!

  • 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-10T19:38:48+00:00Added an answer on June 10, 2026 at 7:38 pm

    Looking at the associations you have defined and simplifying things. Doing a bit refactoring in what we have to achieve.

    Product.rb

    class Product < ActiveRecord::Base
    
      belongs_to :category
    
     end
    

    User.rb

      class User < ActiveRecord::Base
            has_many :categories, through: :category_friendships
            scope :all_data , includes(:categories => [:products])
    
       def get_categories
         categories
       end
    
       def feed
          all_products = Array.new
          get_categories.collect {|category| category.get_products }.uniq
       end
      end
    

    Category.rb

    class Category < ActiveRecord::Base
     has_many :users, through: :category_friendships
     has_many :products
    
     def get_products
       products
     end
    end
    

    NO NEED OF CREATING CATEGORY_FRIENDSHIP MODEL ONLY A JOIN TABLE IS NEEDED WITH NAME CATEGORIES_FRIENSHIPS WHICH WILL JUST HAVE USER_ID AND CATEGORY_ID

    USAGE: UPDATED

    Controller

      class UserController < ApplicationController
             def index
               @all_user_data = User.all_data
            end
       end
    

    view index.html.erb

    <% for user in @all_user_data %>
     <% for products in user.feed %>
      <% for product in products %>
           <%= product.name %>
         end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quite complicated query which will by built up dynamically and is
I have quite complicated query from which I would like to create a view.
This is probably quite trivial but.... I have a quite complicated linq query that
I have a quite complicated HTML/CSS layout which I would like to convert to
That sounds complicated, doesn't it... actually it's quite easy: I have a multi-language app
I have quite a complex piece of code (JQuery and HTML5 Web SQL) that
I have here a quite complicated loop what I would like to modify while
I have to mock quite complicated java web service and I'm searching for the
I have a oracle query and part of it is calculating some value using
This is a more general question: I have a quite complicated files table in

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.