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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:09:01+00:00 2026-05-21T15:09:01+00:00

In a Rails project, i need a user to have an inventory and a

  • 0

In a Rails project, i need a user to have an inventory and a warehouse. Items can be exchanged. The question is, what is the most efficient way to handle this ? I thought of :

User has one inventory -> inventory has many items through inventory_item
User has one warehouse -> warehouse has many items through warehouse_item

However, i’m not so sure whether this is the most efficient way. Another idea would be so have a user_item with a field like location that can either be inventory or warehouse.

What do you think is the best practice ?

  • 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-21T15:09:02+00:00Added an answer on May 21, 2026 at 3:09 pm

    I would probably do a combination of both. I would have an association for the owner for every item and then an association for the place or position of the item. Both of those would probably be polymorphic. I think that would be the best way to fit some of the likely scenarios like:

    1. An item is owned by a player and is placed in the players inventory
    2. An item is owned by a player and is placed in the players warehouse
    3. An item is owner by a player and is equiped by a player (perhaps this is still inventory)
    4. An item is owned by a guild (if this is something you may want in the future) but in a players inventory (borrowed) or equipped

    Thinking of these scenarios I think it is more db query effective to be able to load all items owned by a player without looping through inventories, warehouses etc or to be able to get all items equipped by a player without looping through different owners etc.

    Edit

    Here is an example:

    class User < ActiveRecord::Base
      has_one :inventory
      has_one :warehouse
    
      has_many :owned_items #:as => :owner ???
      has_many :items, :through => :owned_items
    end
    
    class OwnedItem < ActiveRecord::Base
      belongs_to :user #:polymorhpic => :true ???
      belongs_to :position, :polymorphic => true # Could be inventory, warehouse or someplace else
      belongs_to :item
    end
    
    class Inventory < ActiveRecord::Base
      belongs_to :user
      has_many :owned_items, :as => :position
    end
    
    class Inventroy < ActiveRecord::Base
      belongs_to :user
      has_many :owned_items, :as => :position
    end
    

    In the above example I left out the possibility of a guild or something similar being the owner of an item but I added comments on where it could easily be adapted to support such a thing.

    So if you look at the OwnedItem class, position is the polymorphic association the specifies where the item is located at the moment (an inventory, a warehouse, etc) and then it also belongs_to a user which specifies who the owner of the item is. Now you asked about it being redundant and I assume you mean that it could potentially be found by checking the item’s inventory and see who that inventory belongs_to. Yes, of course that is possible. But then it would not be possible to have an item in the inventory that does not belong to you, i.e. a “guild item”.

    But even if you choose not to add something like guilds, I would still recommend to have the OwnedItem belong_to a user. An example to support that would be if you plan on some items being “unique” as in a user can only have one item of that kind. Instead of check both the invetory and warehouse (and possible something else) you can just specify:

    validates_uniqueness_of :item_id, :scope => :user_id
    

    in the OwnedItem class and you’re good to go.

    My point being that all restrictions, queries and comparisons can be made more db time saving if you have both an owner and a position for an item.

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

Sidebar

Related Questions

I'm working on a Rails project and I'm in need of a user-friendly WYSIWYG.
I have a rails project where I need to add some default values to
In a Ruby on Rails project I need to have a JavaScript function executed
I need to install ckeditor in a rails project. I need a page with
I need to do a: require 'generator' Inside of a rails project, but it
Running a ruby on rails project on my mac. I need to test it
I work on a pretty large rails project at work. Sometimes I need to
im working on a rails project version 3 and i can't figure out how
I have a Rails project and use RSpec as a testing framework. What I
I currently have a rails project that looks to let customers authenticate their account

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.