1

has_many :through在某些模型之间建立了关联,这些模型决定了哪些数据集在某些仪表板上可见。

class Dashboard < ActiveRecord::Base
  has_many :dashboard_datasets
  has_many :datasets, :through => :dashboard_datasets
end

class DashboardDataaset < ActiveRecord::Base
  belongs_to :dashboard
  belongs_to :dataset
end

class Dataset < ActiveRecord::Base
  has_many :dashboard_datasets
  has_many :dashboards, :through => :dashboard_datasets
end

创建新的表单Dashboard有一组简单的复选框,命名dataset_ids[]允许您选择我希望在该仪表板上显示哪些预先存在的数据集。

class DashboardForm < Reform::Form
  model: :dashboard

  property :name
  property :description
  collection :dataset_ids
end

到目前为止,很简单...

但是,我现在希望向连接表添加一个额外的关联,以确定应该用于该给定仪表板上的该数据集的“布局”。即网格、表格、列表。ETC

class Layout < ActiveRecord::Base
  has_many :dashboard_datasets
end

class DashboardDataaset < ActiveRecord::Base
  belongs_to :dashboard
  belongs_to :dataset
  belongs_to :layout
end

我现在想调整我的仪表板表单,以便除了复选框之外,对于选中的每个数据集复选框,都有一个选择框供我选择要在此给定关联上使用的布局。

我从哪里开始?扩展collection表单对象以更丰富并包含更多信息?

非常感谢任何建议。

4

1 回答 1

0

0k32在这里找到了一个很好的例子

关键信息如下:

  • 建立一个标准的 has_many 关联,factory -> factory_color -> color
  • 在 factory_color 上使用 accept_nested_attributes_for
  • 创建一个临时模型列表,每个复选框都有一个模型。(factory.all_colors)
  • 使用可以传递关联的 fields_for 功能,以及单独的模型列表,例如 fields_for :factory_colors, @factory.all_colors do |fc| ...
  • 在控制器中添加一个前置过滤器来为未选中的项目设置 _destroy 属性。
于 2018-02-06T22:50:25.630 回答