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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:23:03+00:00 2026-06-09T13:23:03+00:00

My Rails app is a system of Users transacting items between each other. Users

  • 0

My Rails app is a system of Users transacting items between each other. Users bid on items and then the owner approves a bid which creates a transaction. Users can then write comments (messages) on transactions to organise things outside the app. The way I have it structured so far is like so:

User
    has_many :items
    has_many :bids

Item 
    belongs_to :user
    has_many :bids

Bid
    belongs_to :item
    belongs_to :user
    has_one :transaction

Transaction
    belongs_to :bid
    has_many :messages

Message
    belongs_to :transaction
    belongs_to :from, :class_name => "User"
    belongs_to :to, :class_name => "User"

I think this works well in terms of reducing redundancy, but I’m having a few issues here getting data out efficiently. For instance, to retrieve a user‘s “given items” (i.e. users’ items for which there exists a transaction on one of the bids) I am doing:

user.items.joins(:bids).where("bids.id" => Transaction.select("bid_id"))

or received items:

Item.joins(:bids).where("bids.user_id" => user.id).where("bids.id" => Transaction.select("bid_id"))

which seems to be pretty costly for the information I want.

More importantly I want to aggregate users’ transactions and show a ratio of given to received items, which might show up next to a user’s name on any given page. At the moment what I’m doing is fetching and counting all the users’ given and received items above and then dividing (which is extremely costly…). I was thinking of just having a column on the User table with received_count and given_count which updates every time a Transaction is created or destroyed, but this seems unreliable.

My question is, is there a better way to structure my data so that getting information like a user’s transactions is much simpler, while keeping it normalized?

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-06-09T13:23:05+00:00Added an answer on June 9, 2026 at 1:23 pm

    Often normalising and performance are trade offs. In your case I think it makes sense to add the extra columns to the Users table to effectively cache the results. You could also add an extra column to items to show if they have had a bid with a transaction.

    Normalising is really a base principle, and not something you should feel like you need to stick to for correctness, caching commonly used results even if it creates redundancy is also a really good approach.

    The only other option would be to actually use caching (eg. memcached). This would let you store the counts in an actual cache, and just “bust” them when a new transaction occurs for that user.

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

Sidebar

Related Questions

I have a rails app which requires users to verify that they own a
I have few variables which are used system-wide in my rails app. It runs
I have a 'template' system in my CMS rails app. Basically the whole HTML
My rails app allows users to edit a certain json file through the browser.
Simple rails app: I have 2 models, user and intro [which is simply a
My Rails app has some pages which are SSL-required and others which are SSL-optional.
I am putting together a messaging system for a rails app I am working
I've been trying to implement a commenting system for my Rails app. Every event
I am implementing a like system in my rails app using David Celis gem
I'm creating a Rails app which has an e-commerce element for multiple clients -

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.