Rails 5:带有迭代的数据库设计 [英] Rails 5: Database design with iterations
问题描述
我目前有一个包含用户,餐食和订单表的站点。
I currently have a site with Users, Meals, and Orders tables.
- 用户表保存用户信息
- 膳食表中包含膳食名称,描述,img(url)
我当前正在使用迭代:
<% @meals.each do |meal| %>
<%= image_tag(meal.img, alt: meal.name) %>
<span class="mealname"><%= meal.name %></span>
<p><%= meal.description %><p>
<div class="qty">
INPUT FOR ORDER QTYS HERE
</div>
<% end %>
- 订单表包含用户想要的每顿饭的数量
订单迁移以供参考:
t.belongs_to :user
t.integer :total_meals, null: false, default:"0"
t.integer :meal1, null: false, default: "0"
t.integer :meal2, null: false, default: "0"
etc..
在我之前不是使用迭代,而是使进餐1,进餐2等与HTML布局中的餐相匹配。
Before I was not using an iteration and had the respective meal1, meal2, etc matching up to the Meals in the HTML layout.
这显然可以做得更好,但是考虑到我需要膳食信息能够动态更新(我想使用上面的迭代),我应该如何处理呢?
This obviously can be done better, however considering I need the Meals information to be able to be updated dynamically (I want to use the iteration above), How should I go about dealing with this?
但是...如果我使用另一个表,怎么可能将其实现到迭代中并正确填充?
But also... if I use another table, how would I possibly be able to implement that into the iteration and populate correctly?
我可以在Meals表中添加一个 qty列,它属于:用户,b
I could add a 'qty' column to the Meals table and make it belong_to :user, but that is also obviously bad practice and will get really messy really quick.
推荐答案
如果要存储类似以下内容的信息:
If you want to store the information something like:
订单包含餐1,餐2和餐3,且订单属于用户1
Order has meal 1, meal 2 and meal 3 and order belongs to User 1
然后,您应该在这3个模型之间具有正确的关联。 用户
,膳食
和订单
Then you should have the right associations among these 3 models. User
, Meal
and Order
一个订单
可以有很多餐,而一个 Meal
可以属于许多订单,因此 Order
和 Meal
将具有多对多关联
。
One Order
can have many meals, and one Meal
can belong to many orders, So Order
and Meal
will have many-to-many association
.
而订单
将属于
一个用户
解决方案:
将您的订单
模型更改为:
t.belongs_to :user
添加第四个模型,您可以将其命名为 Quantity
:
Add a fourth model, you can name it Quantity
:
t.belongs_to :order
t.belongs_to :meal
t.integer :quantity
现在,假设用户A选择了2个单元A,3个单元B,您将为此用户创建一个订单A,订单A在 Quantity
模型中将有两个条目,其中包含餐号及其各自数量的信息。
Now let's say User A, selects 2 units of Meal A, 3 units of Meal B, you will create an order A, for this user, and Order A will have two entries in Quantity
model containing the info of meal id and their respective quantities.
更新:
模型将如下所示:
Update: Models will look like this:
Order .rb
Order.rb
belongs_to :user
has_many :quantities
has_many :meals, through: :quantities
Quantity.rb
Quantity.rb
belongs_to :order
belongs_to :meal
Meal.rb
has_many :quantities
has_many :orders, through: :quantities
User.rb
has_many :orders
然后,如果您想获取一顿饭的总订单数,可以通过以下方式获得它:
Then, if you want to get the total number of orders one meal has get, you can get it by:
meal.orders.count
,如果要获得一份订单的所有餐点及其数量:
and if you want to get all the meals one order has and their quantities:
order.quantities.each do |record|
record.meal.name # for name of associated meal
record.quantity # for quantity of meal
end
这篇关于Rails 5:带有迭代的数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!