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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:19:23+00:00 2026-05-25T12:19:23+00:00

Given the following models, using rails 2.1: class Product < ActiveRecord::Base has_many :from_sources, :class_name

  • 0

Given the following models, using rails 2.1:

class Product < ActiveRecord::Base
  has_many :from_sources, :class_name => 'ProductSource', :foreign_key => 'to_product_id'
  has_many :to_sources, :class_name => 'ProductSource', :foreign_key => 'from_product_id'

  has_many :from_products, :through => :from_sources
  has_many :to_products, :through => :to_sources
end

class ProductSource < ActiveRecord::Base
  belongs_to :from_product
  belongs_to :to_product
end

class Supplier < ActiveRecord::Base
  has_many :products
end

I really can’t find a way to filter a supplier’s products that comes from another supplier.

Let me give and example with some data

  • Supplier SA has products SAPA, SAPB
  • Supplier SB has products SBPA, SBPB
  • Supplier SC has products SCPA (redistributed from SBPB), SCPB (redistributed from SAPB)

I want to filter :products from supplier SC with only products that comes from SB.

Thanks!

  • 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-25T12:19:24+00:00Added an answer on May 25, 2026 at 12:19 pm

    not sure if i understand well your schema, but i think you will at least need your Product to belong_to a supplier, so you can filter the products table based on which supplier provides it:

    Product.find(:conditions => {:supplier_id => @sb.id})
    

    you can now :

    • find all products WHERE supplier’s id is SC’s id,
    • INNER JOIN them to their from_products,
    • filter the rows WHERE the from_products’ supplier_id is SB’s id,
    • and finally select DISTINCT id on the filtered rows so that you have only one occurence of SCPB.

    so the actual SQL query should look like:

    SELECT DISTINCT products.id, (etc...)
    FROM products, product_sources
      INNER JOIN product_sources
      ON products.from_sources = product_sources.id
      LEFT JOIN products AS source
      ON product_sources.id = source.id
    WHERE products.supplier_id = (SC's id) AND source.supplier_id = (SB's id)
    

    Sorry, but im not really familiar to rails 2 query interface (rails 3 is so sweet),so i can’t tell more, but i think that is the right logic.

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

Sidebar

Related Questions

Given the following models: class Recipe < ActiveRecord::Base has_many :recipe_ingredients has_many :ingredients, :through =>
I'm using Rails 3. I have 3 models: class Deal < ActiveRecord::Base has_many :wishes,
I have the following models with associations as given below:- class Comment < ActiveRecord::Base
I'm struggling with the design of a django application. Given the following models: class
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
Given the following Contribution model: class Contribution(models.Model): start_time = models.DateTimeField() end_time = models.DateTimeField(null=True) is
Given the following model, I want to index the fields (sequence,stock) class QuoteModel(models.Model): quotedate
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following models: class Graph(models.Model): owner = models.ForeignKey(User) def __unicode__(self): return u'%d' %
I have the following model and methods: class UserPrice < ActiveRecord::Base attr_accessible :price, :purchase_date,

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.