【发布时间】:2016-01-14 07:05:08
【问题描述】:
我有当前的模型结构
publisher
---------
has_many :digest_templates
has_one :active_template
digest_templates
---------
belongs_to :publisher, optional: true
数据库结构为:
publishers
---------
t.string :name
t.string :permalink
t.string :api_key
t.string :domain
t.reference :digest_template, index: true, foreign_key: true
digest_templates
---------
t.text :html
t.text :text
t.references :publisher, index: true, foreign_key: true
t.boolean :global, default: false # this is for internally developed templates
在任何给定时间,发布者都可以拥有一个活动模板,但可以为自己创建任意数量的自定义模板。我想这样做,以便可以进行以下调用:
Publisher.templates 和 Publisher.active_template
主要问题是我在同一个表中有全局模板和用户创建的模板。是否有可能使它与这种架构一起工作?还是有更好的做事方式?
【问题讨论】:
-
一个活动模板同时也是一个摘要模板?如果是这样,您将在数据库中保存两次相同的数据。
-
@ArslanAli active_template 只是对处于活动状态的 digest_template 的 ID 引用。
-
这似乎可行。您遇到什么问题?
publisher.templates应该返回什么?它是否应该简单地从has_many关系中返回所有关联digest_templates? -
你是如何指定活动模板的?
标签: ruby-on-rails ruby-on-rails-4 activerecord activemodel