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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:07:39+00:00 2026-06-12T08:07:39+00:00

This is a little bit tricky, so if you need more information, don’t hesitate!

  • 0

This is a little bit tricky, so if you need more information, don’t hesitate!

I have two models, Store and Consumer that are linked by two ways:

1/ Store and Consumer inherite from the same model Profile, because they share many attributes (name, location, email, web page,…). Here is the Rails AR code:

class Profile << ActiveRecord::Base
    # Attributes and validation rules go here.
end

class Store << Profile
end

class Consumer << Profile
end

This is the well known Single Table Inheritance (STI).

2/ In addition to STI, Store and Consumer are linked by a many to many relation:

  • Store has many Clients (many consumers)

  • A consumer is client to many stores

Because I need more attributes for this link (Store – Consumer), I have to create an extra model that will link them: Client.

Here are my final AR models:

class Profile << ActiveRecord::Base
    # Attributes and validation rules go here.
end

class Store << Profile
    has_many :clients
end

class Consumer << Profile
    has_many :clients
end

class Client << ActiveRecord::Base
    belongs_to :store
    belongs_to :consumer
end

Problem

Using STI doesn’t create store_id and consumer_id… we have only profile_id (because one real table Profile). So, how can I target the correct Client row having both store_id and client_id ?

Any idea how to do that? Thanks in advance.

  • 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-12T08:07:40+00:00Added an answer on June 12, 2026 at 8:07 am

    I think you want to do is something like this. Also, I agree with Daniel Wright’s comment.

    class Profile << ActiveRecord::Base
        belongs_to :store
        belongs_to :consumer
    end
    
    class Store << ActiveRecord::Base
        has_one :profile
        has_many :clients
        has_many :consumers, :through => :clients
    end
    
    class Consumer << ActiveRecord::Base
        has_one :profile
        has_many :clients
        has_many :stores, :through => :clients
    end
    
    class Client << ActiveRecord::Base
        belongs_to :store
        belongs_to :consumer
    end
    

    But if you’d like to make it work with what you have you could do something like:

    class Profile << ActiveRecord::Base
    
    end
    
    class Store << Profile
        has_many :clients, :foreign_key => 'store_id'
        has_many :consumers, :through => :clients
    end
    
    class Consumer << Profile
        has_many :clients, :foreign_key => 'consumer_id'
        has_many :stores, :through => :clients
    end
    
    class Client << ActiveRecord::Base
        belongs_to :store
        belongs_to :consumer
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a deceptively tricky little bit of HTML/JS that has either me or
Maybe this sounds a little bit crazy, but I need to come up with
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
its a little bit hard to understand. in the header.php i have this code:
This is getting little bit tricky. I am using state pattern in my application
this is a little bit tricky. usually when someone clicks on a link that
Say I have this little bit of code: public static void LoadSomething(Type t) {
I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
I have this little bit of code that I can't quite work correctly. The
This little bit of syntax has been a bit of a confusion for me

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.