我应该在一个HABTM表中的其他领域? [英] Should I include other fields in a HABTM table?

查看:111
本文介绍了我应该在一个HABTM表中的其他领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个Order对象为包括许多产品的对象,所以我建立在对象HABTM关系。

我不知道它是否正确(或使用Ruby / Rails)的方式还包括HABTM表中的其他数据。举例来说,如果我需要计算小计,有一个机会行项目总数可能需要重写,我存储的关联表的一部分,或者我需要一个LineItem的对象或更好的东西?

感谢

 的ActiveRecord :: Schema.define(版本:3)做

CREATE_TABLE订单,力:真正做| T |
    t.stringORDER_ID,空:假的
    t.stringorder_status,默认:新
    #<剪断>
    t.decimalpay_total,precision:8,规模:2,空:假的
结束

add_index订单,[的order_id],名称:index_orders_on_order_id,独特的:真正的,使用::B树
add_index订单,[order_status],名称:index_orders_on_order_status,使用::B树

CREATE_TABLEorders_products,ID:假的,力:真正做| T |
    t.integerORDER_ID#迁移与belongs_to的
    t.integerPRODUCT_ID#迁移与belongs_to的
    t.decimalpay_cost,precision:8,规模:2,空:假的
    t.decimalpay_discount,precision:8,规模:2,默认值:0.0
    t.decimalpay_linetotal,precision:8,规模:2,空:假的
结束

add_indexorders_products,[的order_id,PRODUCT_ID],名称:index_orders_products_on_order_id_and_product_id,独特的:真正的,使用::B树

CREATE_TABLE产品,力:真正做| T |
    t.string名,空:假的
    t.decimal价格,precision:8,规模:2,空:假的
    t.boolean主动,默认值:true
结束
 

解决方案

连接表(又名HABTM)纯粹用于连接关系和Rails(活动记录)忽略任何附加字段。但是,您可以通过使用的has_many通过的关系,这将是有意义的称之为一个LineItem而不是OrdersProducts解决这个问题。

 类订单
  的has_many:line_items
  的has_many:产品,通过:line_items
结束

一流的LineItem
  belongs_to的:订单
  belongs_to的:产品
结束

类产品
  的has_many:line_items
  的has_many:订单,通过:line_items
结束
 

I would like an Order object to be comprised of many Product objects, so I set up a HABTM relationship on object.

I'm wondering if it's "correct" (or the Ruby/Rails) way to also include additional data within the HABTM table. For instance, if I need to compute the subtotal and there's a chance the line-item totals might need to be overridden, do I store that as part of the association table, or do I need a LineItem object or something better?

Thanks

ActiveRecord::Schema.define(version: 3) do

create_table "orders", force: true do |t|
    t.string   "order_id", null: false
    t.string   "order_status", default: "new"
    # <snip>
    t.decimal  "pay_total", precision: 8, scale: 2, null: false
end

add_index "orders", ["order_id"], name: "index_orders_on_order_id", unique: true, using: :btree
add_index "orders", ["order_status"], name: "index_orders_on_order_status", using: :btree

create_table "orders_products", id: false, force: true do |t|
    t.integer "order_id"  # migrated with belongs_to
    t.integer "product_id"  # migrated with belongs_to
    t.decimal "pay_cost",      precision: 8, scale: 2, null: false
    t.decimal "pay_discount",  precision: 8, scale: 2, default: 0.0
    t.decimal "pay_linetotal", precision: 8, scale: 2, null: false
end

add_index "orders_products", ["order_id", "product_id"], name: "index_orders_products_on_order_id_and_product_id", unique: true, using: :btree

create_table "products", force: true do |t|
    t.string  "name",  null: false
    t.decimal "price",  precision: 8, scale: 2,null: false
    t.boolean "active", default: true
end

解决方案

Join tables (aka HABTM) are purely for joining relationships and Rails (Active Record) ignores any additional fields. However, you can get around this by using a has_many through relationship, which would make sense to call "LineItem" instead of "OrdersProducts".

class Order
  has_many :line_items
  has_many :products, through: :line_items
end

class LineItem
  belongs_to :order
  belongs_to :product
end

class Product
  has_many :line_items
  has_many :orders, through: :line_items
end

这篇关于我应该在一个HABTM表中的其他领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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