好的,所以我有Option的分组到OptionGroup的。 OptionGroup有一个名为:multiple的布尔属性,用于确定是否可以选择组中的多个选项,或者只选择组中的单个选项。我还有一个LineItem模型,它与Option模型有很多关系。
以下是我的模特
class OptionGroup < ApplicationRecord
# Uses :multiple, a boolean attribute to determine if multiple can be selected
has_many :options
end
class Option < ApplicationRecord
belongs_to :option_group
end
class LineItem < ApplicationRecord
has_many :line_item_options
has_many :options, through: line_item_options
end
class LineItemOption < ApplicationRecord
belongs_to :option
belongs_to :line_item
end
我在这里遇到的问题是,某些选项组只允许选择组中的单个选项(单选按钮),而某些option_groups将允许选择任何选项组合。
在LineItemsController中我有:
private
def line_item_params
params.require(:line_item).permit(:some, :list, :of, :params, options_ids: [])
在我的line_item表单中,我如何根据选项的option_group.multiple属性在单选按钮和复选框之间切换?
答案 0 :(得分:2)
这称为has_many :through
关联,更新Option
类,如下所示
class Option < ApplicationRecord
belongs_to :option_group
has_many :line_item_options
has_many :line_items, through: line_item_options
end