Rails / postgres,’外键’存储在数组中以创建1-many关联

postgres数组可以用来在rails(4)中创建一对多/ has_many关联吗? 我知道外键类型数组是不可能的。

示例:任务有多个受理人。 传统上我会使用关联表来解决这个问题:tasks-> assignees-> users。 使用数组,这不是必需的,因为可以存储多个“外键”。

然后可以使用以下查询来获取分配给我的所有任务:

select * from tasks where ? IN tasks.assignees 

您将无法使Rails知道此数组并将其用于关联。

但是,如果您希望更快地搜索/过滤分配给用户的任务,您可以在Task对象中保留一组用户ID。 否则,您必须在标准关联表中执行JOIN以查找分配给Alice的所有任务。

因此,解决方案是保留关联表,同时将受理人用户ID复制到Task对象中,并使用该ID列表进行更快的搜索/过滤。

您需要挂钩受理对象的after_createafter_destroy生命周期,并将新的受理人ID插入到任务记录数组中。 然后,当从任务中删除一个asignee时,更新该arrays以删除该ID。

有关所有Array运算符,请参阅Postgres文档:

像这样的东西:

 class Task < ActiveRecord::Base has_many :assignees, :dependent => :destroy end class Asignee < ActiveRecord::Base belongs_to :task after_create :insert_task_assignee after_destroy :remove_task_assignee # assumes that there is a column called assignee_id # that contains the User ID of the assigned person private def insert_task_assignee # TODO: check for duplicates here - before we naively push it on? task.assignee_list = task.assignee_list.push(assignee_id) task.assignee_list.save end def remove_task_assignee id_list = task.assignee_list id_list.reject! { |candidate_id| candidate_id == assignee_id } task.assignee_list = id_list task.assignee_list.save end end # find all tasks that have been assigned Alice & Bob # this will require the `postgres_ext` gem for Ruby / Postgres array searching tasks = Task.where.contains(:assignee_list => [alice.id, bob.id]).all