我发现了一个关于 Postgres 的新东西:复合类型。我真的很喜欢这种方法,它对我非常有用。
问题是 Rails 的 ActiveRecord 没有对此的原生支持。
你曾经在 Rails 中使用过 Postgres 的复合类型吗?是一次很好的体验,还是您更喜欢为这个嵌套数据创建新模型的常用方法?
http://www.postgresql.org/docs/8.4/static/rowtypes.html
谢!:-)
我发现了一个关于 Postgres 的新东西:复合类型。我真的很喜欢这种方法,它对我非常有用。
问题是 Rails 的 ActiveRecord 没有对此的原生支持。
你曾经在 Rails 中使用过 Postgres 的复合类型吗?是一次很好的体验,还是您更喜欢为这个嵌套数据创建新模型的常用方法?
http://www.postgresql.org/docs/8.4/static/rowtypes.html
谢!:-)
这是 PostgreSQL 的一个有趣的特性,但是我没有机会使用它。
在 Rails 方面,我想到了一些事情:
在数据库方面想到了一些事情:
除非您想到的特定应用程序具有令人信服的好处,否则我会建议采用更规范的方法。代替:
CREATE TYPE inventory_item AS (
name text,
supplier_id integer,
price numeric
);
CREATE TABLE on_hand (
item inventory_item,
count integer
);
INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);
您可以通过执行以下操作来获得类似的结果,同时保持对 ActiveRecord 的完全支持,而无需扩展 Postgres 适配器或创建自定义类:
CREATE TABLE inventory_item (
id integer,
name text,
supplier_id integer,
price numeric
);
CREATE TABLE on_hand (
inventory_item_id integer,
count integer
);
INSERT INTO inventory_item VALUES ('fuzzy dice', 42, 1.99) RETURNS INTEGER;
INSERT INTO on_hand VALUES (<inventory_item_id>, 1000);