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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:20:03+00:00 2026-05-18T07:20:03+00:00

Hey guys, I’m at a deadlock here after thinking about this for too long.

  • 0

Hey guys, I’m at a deadlock here after thinking about this for too long.

Context: Given the following models:

  • User
  • Item
  • Lock

Here’s the scenario: A lock is basically like a ‘hold’. A user can place a ‘lock’ on any given item to signal to the system that the item should not be deleted. Items wont be deleted until the lock is cleared.

Here’s the tricky part. The lock is its own model because I want multiple users to be able to lock any given item. So let’s say Bob locks an item, one didn’t already exist so it creates a lock for that item, and information stating that Bob is currently associated with that lock. John comes and locks the same item, but a lock already exists, so John is simply ‘added under’ the same lock. The lock won’t be removed until all users choose to ‘unlock’, or disassociate themselves with that lock.

My confusion is how I should model these relationships. A user can of course have many locks, each associated with a different item (since any given item can have at most one lock). The locks themselves can have many users. From the point of view of the item, each item can have one lock associated with many users.

So in other words, I would like to access the information a little something like this:

item.lock.users # get the users 'locking' the item
user.locks # get the items the user is currently 'locking

Perhaps the separate Lock model isn’t required, but I figured it would be in order to signify that multiple users can be locking a particular item.

I think what further complicates things is that items are added by users, so I would want to have a way to access the items by a user for example user.items or item.user.

Right now I have:

  • user has and belongs to many locks
  • lock has and belongs to many users
  • user has many items
  • item belongs to user
  • item has one lock
  • lock belongs to item

Does this seem correct?

  • 1 1 Answer
  • 2 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-18T07:20:03+00:00Added an answer on May 18, 2026 at 7:20 am

    I think what you’re doing will work though you may not have to use the habtm. What if an item can have many locks and can only be deleted when it has no locks. That way you could add a date/reason/comment for each lock by user.

    • user
      • has_many :locks
      • has_many :items
    • lock
      • belongs_to :user
      • belongs_to :item
    • item
      • belongs_to :user
      • has_many :locks

    This will still allow you to do user.locks though item.lock.users won’t work, but by looking at each lock you’ll easily be able to get the users.

    item.locks.each do |lock|
        puts lock.user
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
Hey Guys I'm running this little function here function getBeaches() { $request=Slim::getInstance()->request(); $args=filter_var_array(func_get_args(),FILTER_SANITIZE_STRING); $sql=SELECT
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey guys I've written this MS SQL script with my awesome MS SQL skills,
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys I have about 8 fieldSets and Im iterating over a list. I
Hey Guys I've been struggling with this for the last hour and pouring over
Hey guys I just noticed that my join statement here SELECT * FROM reports
Hey guys I can't seem to get the syntax here right, I'm just trying

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.