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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:04:52+00:00 2026-05-30T19:04:52+00:00

I have a situation where I have a SQL table which pretty much ‘parents’

  • 0

I have a situation where I have a SQL table which pretty much ‘parents’ others.
It is easiest to imagine in the example below where there are different types of clients (which need different fields of data to be stored) that all have a unique client ID.

CREATE TABLE clients (
  id serial NOT NULL,
  internal_ref character varying,
  address character varying,
  telephone_no character varying,
  CONSTRAINT pkey_clients PRIMARY KEY (id),
  CONSTRAINT uniq_clients_internal_ref UNIQUE (internal_ref)
)

CREATE TABLE partnerships (
  id serial NOT NULL,
  clients_id integer,
  name character varying,
  CONSTRAINT pkey_partnerships PRIMARY KEY (id),
  CONSTRAINT fkey_partnerships_clients_id FOREIGN KEY (clients_id)
    REFERENCES clients(id) MATCH FULL
      ON UPDATE NO ACTION
      ON DELETE NO ACTION
)

CREATE TABLE individuals (
  id serial NOT NULL,
  clients_id integer,
  forename character varying,
  surname character varying,
  CONSTRAINT pkey_individuals PRIMARY KEY (id),
  CONSTRAINT fkey_individuals_clients_id FOREIGN KEY (clients_id)
    REFERENCES clients(id) MATCH FULL
      ON UPDATE NO ACTION
      ON DELETE NO ACTION
)

I’m new to Active Record and not sure how (or if) I can elegantly implement it, so that I can find a business and I get the joined client data.

Currently I am trying:

class Client < ActiveRecord::Base
end
class Partnership < ActiveRecord::Base
  has_one :clients
end

output = Partnership.find(:all) # I only have one row in both tables, which is also joinable
p output # => [#<Partnership id: 1, name: "PART1", clients_id: 1>]

I need output to also have the joinable data, such as internal_ref, address etc. How can I do 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-30T19:04:54+00:00Added an answer on May 30, 2026 at 7:04 pm

    Easy. Your “child” models belongs_to “parent” model which can has_one or has_many “children”

    Models:

    # app/models/client.rb
    class Client < ActiveRecord::Base
      has_one :partnership
      has_one :individual
    end
    
    # app/models/pertnership.rb
    class Partnership < ActiveRecord::Base
      belongs_to :client
    end
    
    # app/models/individual.rb
    class Individual < ActiveRecord::Base
      belongs_to :client
    end
    

    Controller:

    @output = Partnerships.all
    

    If you don’t want to make additional query each time you try to access the Client you use eager loading:

    @output = Partnerships.includes(:client).all
    

    Or eager loading with both client and individual:

    @output = Partnerships.includes(:client => :individual ).all
    

    Then, in your view, you can easily access referenced client or even individual referenced in client.

    <% @output.each do |partnership| %>
      <%= partneship.client.internal_ref %>
      <%= partneship.client.address %>
      <%= partneship.client.individual.forname %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table T (structure below) which initially contains all-NULL values in an
I have an EMPLOYEE table in a SQL Server 2008 database which stores information
I have a situation in which I have taken a backup(sql scripyts containing the
I guess this is strange situation. I have a results table which contain 100k
I have a SQL situation for which I don't have a solution. Combinations of
We have a situation where our application calls some stored procedures on a sql
I have a situation where our developers extended a Third party database (MS SQL)
I am using SQL Server 2000, I have a situation where I am copying
Do T-SQL queries in SQL Server support short-circuiting? For instance, I have a situation
I have an unusual situation to model in a MS SQL Server database: the

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.