【发布时间】:2017-06-13 20:39:43
【问题描述】:
我使用单表继承来模拟 Stores、Tailors、Orders 和 TailorOrders。订单属于裁缝,但我无法在 rails 控制台中访问裁缝的订单。商店有多种类型,在数据库中订单有requester_id 和provider_id。裁缝与订单的关系始终是提供者。继承模型如下:
class Store
end
class Tailor < Store
has_many :orders
end
class Order
has_many :items
end
class TailorOrders
belongs_to :tailor, class_name: "Tailor", foreign_key: "provider_id"
end
class Item
belongs_to :order
end
我能够获取属于订单的商品,如下所示:
Order.first.items
#=> [#<Item:0x007fe9f0cc8f58
id: 1,
order_id: 1,
name: "Grey Pants",
created_at: Tue, 13 Jun 2017 17:50:57 UTC +00:00,
updated_at: Tue, 13 Jun 2017 17:50:57 UTC +00:00,
type_id: 1>]
而且,我可以从订单中得到裁缝:
Order.first.tailor
#=> #<Tailor:0x007fe9f214b8b8
id: 3,
company_id: 3,
primary_contact_id: 3,
phone: "2121468958",
street1: "640 Bennie Way",
street2: "Apt. 533",
city: "Carliefurt",
state: "New Mexico",
zip: "42439-4809",
country: "Ukraine",
created_at: Tue, 13 Jun 2017 17:50:56 UTC +00:00,
updated_at: Tue, 13 Jun 2017 17:50:56 UTC +00:00,
type: "Tailor",
name: "Joe's on Main Street">
但是,我不能得到属于裁缝的订单:
Tailor.first.orders
#=> #<Order::ActiveRecord_Associations_CollectionProxy:0x3ff4f786e9e0>
Tailor.first.orders.first
#=> ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column orders.tailor_id does not exist
LINE 1: SELECT "orders".* FROM "orders" WHERE "orders"."tailor_id" ...
这个错误似乎与我上面描述的数据库布局有关,其中类 Tailor 在订单的 db 表中始终是 provider。
有没有办法通过裁缝(一种商店,也是订单表中的提供者)访问订单?我还想完成类似于Stores.first.orders.first.items 的事情。任何帮助将不胜感激!
【问题讨论】:
-
在
Tailor类中为has_many :orders关联设置foreign_key选项。现在它期望外键是orders表中的tailor_id列,在您的情况下是不同的。 -
orders表中是否有一列指向它所属的Tailor? -
@chumakoff 它有效!完美谢谢
标签: ruby-on-rails ruby postgresql activerecord single-table-inheritance