简体   繁体   English

如何在数据库中为块创建新行,或者在Rails中为每个模型创建新模型?

[英]How do you create a new row for a block in a database or a new model for each in Rails?

I'm creating a baby stork party / baby shower gift registry app with Rails where the mom to bee creates a registry and the other friends can nominate gifts etc. 我正在用Rails创建一个婴儿鹳派对/婴儿送礼会礼物注册表应用程序,其中要养的蜜蜂创建一个注册表,其他朋友可以提名礼物等。

I'm using Rails and after studying a few Rails books I'm getting to intermediate. 我正在使用Rails,在学习了几本Rails书籍之后,我就进入了中级阶段。 The problem is I can't figure out how to create a fixed baby registry where the mom can just choose quantity, color, brand next to each product category and save. 问题是我不知道如何创建固定的婴儿注册表,妈妈只能在每个产品类别旁边选择数量,颜色,品牌并保存。

I created the model Registry and the columns quantity, brand, color etc. After a while I figured this won't work as how will a name each category (row) for those columns and keep it fixed (in a sense) and separate almost like a form? 我创建了注册表模型,并创建了列数量,品牌,颜色等模型。过了一会儿,我发现这将无法正常工作,因为如何为这些列命名每个类别(行),并使其(在某种意义上)保持固定并几乎分开像表格? It almost feels like I need a separate model for each category but that can't be the most effective way? 几乎感觉每个类别我都需要一个单独的模型,但这不是最有效的方法吗?

Its really hard to explain, but I'm stuck or missing something? 真的很难解释,但是我卡住了或错过了什么?

It sounds like you are describing a many-to-many relationship between a registry item, and a trait. 听起来您正在描述注册表项和特征之间的多对多关系。 So an item can have many traits (color, quality, brand) and a trait can have many items (so you can find all the items by color, quality, etc). 因此,一个项目可以具有许多特征(颜色,质量,品牌),而一个特征可以具有许多项目(因此您可以按颜色,质量等找到所有项目)。

Checkout: 查看:

http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

class Color
  has_and_belongs_to_many :items
end

class Items
  has_and_belongs_to_many :colors
end

Will allow you to do things like: 将允许您执行以下操作:

item = Item.find 1
color = Color.find_by_name 'blue'
item.colors << color

# all the items for a color
color.items    # item 1

# all the colors for an item
item.colors    # blue

Structuring your form(s) to support this will require some work, I suggest googling around for examples. 构建您的表单以支持此操作将需要一些工作,我建议四处搜寻示例。

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

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