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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:02:56+00:00 2026-05-23T14:02:56+00:00

I have the following models: Technician – id – name – personal_id Tool –

  • 0

I have the following models:

Technician
- id
- name
- personal_id

Tool
- id
- internal_code
- name
- category

A technician can have many tools and one tool can be assigned to many technician (do I really need many-to-many relationship, as I’m typing this I have my doubts). My problem is that I also need to track the quantity of the tool that the technician have under his possession. One record should be like [technician_id, tool_id, quantity]. I have read about the has_many through, but i have not seen an example that fit my problem, what should the structure of the resulting table and also how should I set up the model for tools under the technician possession?

  • 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-23T14:02:56+00:00Added an answer on May 23, 2026 at 2:02 pm

    It seems like you are looking for a many-to-many association here.
    This should work for you given the requirements you set:

    class Technician < ActiveRecord::Base
      has_many :assignments
      has_many :tools, :through => :assignments
    end
    
    class Tool < ActiveRecord::Base
      has_many :assignments
      has_many :technicians, :through => :assignments
    end
    
    class Assignment < ActiveRecord::Base
      belongs_to :tool
      belongs_to :technician
    end
    

    Hope it helps.


    UPDATE

    Thinking about your comment about having a quantity field in Assignments, I’m assuming you are wanting to be able to assign many of the same Tool to a Technician. For example:

    >> Technician.new(:name => "Bob").save
    => true
    >> Tool.new(:name => "hammer").save
    => true
    >> Tool.new(:name => "saw").save
    => true
    >> t = Technician.first
    => #<Technician id: 1, name: "Bob", personal_id: nil, created_at: "2011-07-07 22:56:53", updated_at: "2011-07-07 22:56:53">
    >> t.tools << [Tool.first, Tool.last]
    => [#<Tool id: 1, name: "hammer", internal_code: nil, category: nil, created_at: "2011-07-07 22:56:22", updated_at: "2011-07-07 22:56:22">, #<Tool id: 2, name: "saw", internal_code: nil, category: nil, created_at: "2011-07-07 22:56:30", updated_at: "2011-07-07 22:56:30">]
    >> t.save
    => true
    >> t.assignments
    => [#<Assignment id: 1, technician_id: 1, tool_id: 1, quantity: nil, created_at: "2011-07-07 23:01:53", updated_at: "2011-07-07 23:01:53">, #<Assignment id: 2, technician_id: 1, tool_id: 2, quantity: nil, created_at: "2011-07-07 23:02:46", updated_at: "2011-07-07 23:02:46">]
    >> a = t.assignments.first
    => #<Assignment id: 1, technician_id: 1, tool_id: 1, quantity: nil, created_at: "2011-07-07 23:01:53", updated_at: "2011-07-07 23:01:53">
    >> a.quantity = 5
    => 5
    >> a.save
    => true
    >> t.assignments.first.quantity
    => 5
    >> 
    

    If this is not the case, let me know & I can update with another approach.

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

Sidebar

Related Questions

I have the following models: class Category(models.Model): name = models.CharField(max_length=40) class Item(models.Model): name =
I have the following models: Game and Pick. There's a one to many association
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following models in my Django app. How can I from the
I have the following models: -> = 1:many, <-> = many:many User <-> Product
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
I have the following models (simplified): class Location(models.Model): name = models.CharField(max_length=100) is_ok = models.BooleanField()
I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True,
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views

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.