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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:53:07+00:00 2026-05-10T20:53:07+00:00

Suppose the following data schema: Usage ====== client_id resource type amount Billing ====== client_id

  • 0

Suppose the following data schema:

Usage ====== client_id resource type amount  Billing ====== client_id usage_resource usage_type rate 

In this example, suppose I have multiple resources, each of which can be used in many ways. For example, one resource is a widget. Widgets can be fooed and they can be bared. Gizmos can also be fooed and bared. These usage types are billed at different rates, possibly even different rates for different clients. Each occurence of a usage (of a resource) is recorded in the Usage table. Each billing rate (for client, resource, and type combination) is stored in the billing table.

(By the way, if this data schema is not the right way to approach this problem, please make suggestions.)

Is it possible, using Ruby on Rails and ActiveRecord, to create a has_many relationship from Billings to Usages so that I can get a list of usage instances for a given billing rate? Is there a syntax of the has_many, :through that I don’t know?

Once again, I may be approaching this problem from the wrong angle, so if you can think of a better way, please speak up!

  • 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. 2026-05-10T20:53:08+00:00Added an answer on May 10, 2026 at 8:53 pm

    There is apparently an project at sourceforge to extend Rails’ ActiveRecord with support for Composite Primary Keys. I haven’t used this extension, but it might help you. It’s also a gem at rubyforge.

    Plain Ruby on Rails, as of version 2.0, does not support compound primary keys (cf. HowToUseLegacySchemas). Every table must have a single-column, auto-increment key named ‘id‘.

    The explanation I have seen is: ‘You only need compound primary keys if you want to use a legacy database.’ This is of course a ridiculously ignorant view of data modeling.

    The solution I see would be:

    • Usage.client_id -> Client.id
    • Usage.type_id -> Usagetype.id
    • Usage.resource_id -> Resource.id
    • Billing.usage_id -> Usage.id
    • Billing.client_id -> Client.id
    • Billing.type_id -> Usagetype.id
    • Billing.resource_id -> Resource.id

    The apparently redundant foreign keys in Billing attempt to enforce partial referential integrity. But it doesn’t quite get there — it does not prevent you from creating rows in Billing that reference a row in Usage with the wrong client/resource/usagetype combination, not matching those in the referencing row in the Billing table.

    edit: @Yarik: yes, you’re right. It makes more sense for Usage to reference Billing.

    • Usage.billing_id -> Billing.id

    Hmm. I made an ER diagram but I’m having trouble inserting it as an image.

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

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Just add a div that only IE6 users see. <!--[if… May 11, 2026 at 3:10 pm
  • added an answer Have you done any studies on existing recipes? A varchar(4000)… May 11, 2026 at 3:10 pm
  • added an answer without the source it's very hard to help you. Further… May 11, 2026 at 3:10 pm

Related Questions

I'm implementing a tagging system for a website. There are multiple tags per object
Suppose the following configuration: Drive D ... Data, Drive E .... TempDB, Drive F
I need to query a table for values given a string. The table is
Suppose i have the following class. public class Location { public Id { get;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.