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

Xea*_*ago 11 ruby postgresql ruby-on-rails

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

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

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

select * from tasks where ? IN tasks.assignees
Run Code Online (Sandbox Code Playgroud)

Cod*_*lan 6

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

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

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

你需要挂接到after_createafter_destroy生命周期受让人对象和插入新的受让人ID添加到任务记录阵列.然后,当从任务中删除一个asignee时,更新该阵列以删除该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
Run Code Online (Sandbox Code Playgroud)