I would solve this by implementing a third model that maintains a relationship between products and a list. And then you can process it in forms with :accepts_nested_attributes_for.
To give an example, here is how I would structure the models:
class List < ApplicationRecord
has_many :list_items, inverse_of: :list
has_many :items, through: :list_items
accepts_nested_attributes_for :list_items, reject_if: proc { |attr| attr[:item_id].blank? }
end
class Item < ApplicationRecord
has_many :list_items
has_many :lists, through: :list_items
end
class ListItem < ApplicationRecord
belongs_to :list, inverse_of: :list_items
belongs_to :item
end
, .
<h1>New List</h1>
<%= form_for @list do |f| %>
<% @items.each_with_index do |item, i| %>
<%= f.fields_for :list_items, ListItem.new, child_index: i do |list_item_form| %>
<p>
<%= list_item_form.check_box :item_id, {}, item.id, "" %> <%= item.name %>
</p>
<% end %>
<% end %>
<p>
<%= f.submit 'Create List' %>
</p>
<% end %>
, , @items - , , . , FormBuilder fields_for.
, :child_index , (.. name="list[list_item_attributes][0][item_id]") , , .
FormBuilder check_box :
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
, , , , item.id, , . accepts_nested_attributes_for List, , , :item_id , ListItems .
, , , :
def allowed_params
params.require(:list).permit(list_items_attributes: [:item_id])
end