如何实现RoR的许多一对多的关系?` [英] how to implement RoR many-to-many relationship?`

查看:111
本文介绍了如何实现RoR的许多一对多的关系?`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

Technician
- id
- name
- personal_id

Tool
- id
- internal_code
- name
- category

一个技术人员可以有很多工具和一个工具可以分配给许多技术人员(我真的需要很多一对多的关系,因为我打字这一点,我有我的怀疑)。我的问题是我还需要跟踪的技术人员都在他的掌握工具的数量。一条记录应该是这样的[technician_id,tool_id,数量。我看了一下经过的has_many,但我还没有看到适合我的问题的例子,我应该得到的表的结构,也是我应该怎么建立模型为工具,在技术人员身上?

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?

推荐答案

好像你正在寻找一个多一对多的关联在这里。 这应该为你工作给你提出的要求:

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.

更新

思考你的关于作业有一个量的现场评论,我假设你是想能够分配许多相同的工具技术员。例如:

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.

这篇关于如何实现RoR的许多一对多的关系?`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆