如何将 collection_check_boxes 与序列化数组一起使用

How to use collection_check_boxes together with serialized Array

class Category < ActiveRecord::Base
  serialize :blocked_user_ids, Array
end

查看:

<%= collection_check_boxes(:category, :blocked_user_ids, User.all, :id, :full_name) %>

被阻止用户的 ID 存储在序列化属性中 blocked_user_ids

将所有 collection_check_boxes 用户的 checked/unchecked 框传递给控制器​​效果很好。

我想知道如何为存储在 blocked_user_ids 数组中的每个 ID 显示一个复选框?

<%= collection_check_boxes(:category, :blocked_user_ids, User.all, :id, :full_name) %>

然后会变成

# Probably NOT a good idea to interate through all Users!
- User.all.each do |user|
  # assuming user has a `name` attribute
  %label
    = check_box_tag "category[blocked_user_ids][]",
      value = "#{user.id}",
      checked = (@category.block_user_ids.include? user.id.to_s),
      id: "blocked_user_ids_#{user.id}"
    "#{user.name}"