【问题标题】:Checkboxes for has_many through, with additional join table attributehas_many 的复选框,带有附加的连接表属性
【发布时间】:2018-03-03 00:15:12
【问题描述】:

我在一些模型之间有一个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

到目前为止,一切都很简单......

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

class Layout < ActiveRecord::Base
  has_many :dashboard_datasets
end

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

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

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

非常感谢任何建议。

【问题讨论】:

    标签: ruby-on-rails reform trailblazer


    【解决方案1】:

    Found a good example here0k32

    关键信息是:

    • 设置标准 has_many 关联,工厂 -> factory_color -> color
    • 在 factory_color 上使用accepts_nested_attributes_for
    • 创建一个临时模型列表,每个复选框都有一个模型。 (factory.all_colors)
    • 使用可以传递关联的 fields_for 功能和单独的模型列表,例如fields_for :factory_colors, @factory.all_colors 做 |fc| ...
    • 在控制器中添加一个前置过滤器,为未选中的项目设置 _destroy 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2013-04-26
      • 2011-07-18
      • 2018-08-18
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多