简体   繁体   English

Rails 3.1 Active Record关联可能与嵌套形式

[英]Rails 3.1 Active Record associations possibly with nested forms

I have a question regarding a simple app I am developing in Rails 3.1, but I am not sure of how to define the associations and am looking for advice on the best way to achieve the following. 对于在Rails 3.1中开发的一个简单应用程序,我有一个问题,但是我不确定如何定义关联,并且正在寻求实现以下目标的最佳方法方面的建议。

I have tapes, each tape contains a barcode. 我有磁带,每个磁带都包含一个条形码。

I have boxes, each box has a name 我有盒子,每个盒子都有一个名字

What i need to track is the act of associating the two , each day up to 24 tapes will be placed into a Box, so i need a way of creating a new association for existing boxes and existing tapes on a form, and ideally track some extra information. 我需要跟踪的是将两者关联的动作,每天最多可以将24个磁带放入一个Box中,因此我需要一种为现有盒子和表单上的现有磁带创建新的关联的方法,并且理想地跟踪一些额外的信息。

The tapes are on a 5 week cycle, so after 5 weeks a tape will ( possibly ) be associated with a new box. 磁带以5周为一个周期,因此5周后,磁带将(可能)与新包装盒关联。

I am not sure how to model the associations, any help or input would be appreciated 我不确定如何建立关联模型,任何帮助或输入将不胜感激

Do you need to track which box it used to belong to? 您需要跟踪它以前属于哪个盒子吗? If not, this is a simple belongs_to/has_many association. 如果不是,这是一个简单的belongs_to / has_many关联。

class Tape
  belongs_to :box
end
class Box
  has_many :tapes
end

If you need to track past boxes, then you'll need a join-model: 如果需要跟踪过去的框,则需要一个连接模型:

class WhateverYouWant
   belongs_to :box
   belongs_to :tape
end

and change the box/tape associations to use ":through" 并将box / tape关联更改为使用“:through”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM