Rails 4 Reflexive多对多关联

时间:2014-09-10 23:51:54

标签: ruby-on-rails-4 many-to-many rails-activerecord model-associations

我实现了一个Rails 4应用程序,该应用程序具有Reflexive多对多关联来表示一些MailChimp和HelpScout API参数。

其中一些参数具有子参数。因此,那些子参数有父参数(否则他们不能是孩子,对吗?!; D)。

为了实现自反关联,创建了以下两个表

create_table "api_params", force: true do |t|
  t.string   "name"
  t.string   "description"
  t.string   "type"
  t.boolean  "required"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "nested_params", force: true do |t|
  t.integer  "parent_param_id"
  t.integer  "child_param_id"
  t.boolean  "required"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "nested_params", ["child_param_id"], name: "index_nested_params_on_child_param_id"
add_index "nested_params", ["parent_param_id"], name: "index_nested_params_on_parent_param_id"

我想要的是两种方法。一个用于检索数据库记录的父级,另一个用于检索其子级。下面我给你举个例子。

apiParam = ApiParam.first # retrieving any database record
apiParam.parent_params
# => returns a set of apiParam's parents
apiParam.child_params
# => returns a set of apiParam's children

我整天都在阅读关于协会的内容,但这些例子总是一样的。我的意思是,总有一个类定义了一个has_many,另一个定义了belongs_to,但它不足以满足我的需要。

提前致谢。我感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我有你的解决方案(在我的情况下我有模型SyncType):

class SyncType < ActiveRecord::Base
  has_and_belongs_to_many(:parents,
    :class_name => "SyncType",
    :join_table => "sync_type_parents",
    :foreign_key => "sync_type_id",
    :association_foreign_key => "sync_type_parent_id")

  has_and_belongs_to_many(:children,
    :class_name => "SyncType",
    :join_table => "sync_type_parents",
    :foreign_key => "sync_type_parent_id",
    :association_foreign_key => "sync_type_id")
end 

迁移:

create_table "sync_type_parents", :force => true, :id => false do |t|
  t.integer "sync_type_id", :null => false
  t.integer "sync_type_parent_id", :null => false
end

享受! ;)

答案 1 :(得分:0)

我找到了答案,我希望它对每个遇到同样问题的人都有帮助。

简而言之,我从Ryan Bates的railscasts.com网站得到了答案。更具体地说,答案是here

我的课程现在如下:

class NestedParam < ActiveRecord::Base
  belongs_to :api_param
  belongs_to :child_param, class_name: 'ApiParam'
end

class ApiParam < ActiveRecord::Base
  has_many :nested_params
  has_many :child_params, :through => :nested_params
  has_many :parent_api_params, :class_name => "NestedParam", :foreign_key => "child_param_id"
  has_many :parent_params, :through => :parent_api_params, :source => :api_param
end

ActiveRecord::Schema.define(version: 20140910175658) do

  create_table "api_params", force: true do |t|
    t.string   "name"
    t.string   "description"
    t.boolean  "required",    default: true
    t.boolean  "struct",      default: false
    t.string   "type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "nested_params", force: true do |t|
    t.integer  "api_param_id"
    t.integer  "child_param_id"
    t.boolean  "required"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "nested_params", ["api_param_id"], name: "index_nested_params_on_api_param_id"
  add_index "nested_params", ["child_param_id"], name: "index_nested_params_on_child_param_id"
end