在Mongoid中从另一个模型引用相同模型3次

时间:2014-08-01 19:57:37

标签: ruby activerecord mongoid mongoid3

我有两个模型如下:

class Tag
  include Mongoid::Document
  include Mongoid::Timestamps
  belongs_to :group
end

class Group
  include Mongoid::Document
  include Mongoid::Timestamps

  has_one :user_tag, :class_name => 'Tag', :foreign_key => "user_tag_id", :inverse_of => nil
  has_one :tag_tag, :class_name => 'Tag', :foreign_key => "tag_tag_id", :inverse_of => nil
  has_one :group_tag, :class_name => 'Tag', :foreign_key => "group_tag_id", :inverse_of => nil
end

但是这种和其他尝试似乎没有产生3种不同类型的组可单独引用的预期结果。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我认为以下是您想要的,尽管有三个额外的琐碎模型/子类。 我尝试仅使用Tag类进行此操作,并且没有子类遇到冲突和其他问题 具有反比关系,即使放宽了多态性的限制。 也许三个额外的琐碎子类可以用于您的目的? 请注意,标签数据仍存储在一个标签中。由' store_in'指定的集合方法

应用程序/模型/ group.rb

class Group
  include Mongoid::Document
  include Mongoid::Timestamps
  field :name, type: String

  has_one :user_tag
  has_one :tag_tag
  has_one :group_tag
end

应用程序/模型/ tag.rb

class Tag
  include Mongoid::Document
  include Mongoid::Timestamps
  field :text, type: String

  belongs_to :group
  store_in collection: 'tag'
end

应用程序/模型/ group_tag.rb

class GroupTag < Tag
end

应用程序/模型/ tag_tag.r

class TagTag < Tag
end

应用程序/模型/ user_tag.rb

class UserTag < Tag
end

测试/单元/ group_test.rb

require 'test_helper'
require 'pp'

class GroupTest < ActiveSupport::TestCase
  def setup
    Mongoid.default_session.drop
  end
  test '0. mongoid version' do
    puts "\nMongoid::VERSION:#{Mongoid::VERSION}\nMoped::VERSION:#{Moped::VERSION}"
  end
  test 'multiple tag references' do
    group = Group.create(name: 'my group')
    group.user_tag = UserTag.create(text: 'user tag')
    group.tag_tag = TagTag.create(text: 'tag tag')
    group.group_tag = GroupTag.create(text: 'group tag')
    assert_equal(1, Group.count)
    assert_equal(3, Tag.count)
    puts
    puts "Group:"
    pp Group.all.to_a
    puts "Tag:"
    pp Tag.all.to_a
    puts "collections: #{Mongoid.default_session.collection_names.inspect}"
  end
end

rake test

运行选项:

正在运行测试:

[1/2] GroupTest#test_0._mongoid_version
Mongoid::VERSION:3.1.6
Moped::VERSION:1.5.2
[2/2] GroupTest#test_multiple_tag_references
Group:
[#<Group _id: 53f242b27f11ba5f31000001, created_at: 2014-08-18 18:15:14 UTC, updated_at: 2014-08-18 18:15:14 UTC, name: "my group">]
Tag:
[#<UserTag _id: 53f242b27f11ba5f31000002, created_at: 2014-08-18 18:15:14 UTC, updated_at: 2014-08-18 18:15:14 UTC, text: "user tag", group_id: "53f242b27f11ba5f31000001", _type: "UserTag">,
 #<TagTag _id: 53f242b27f11ba5f31000003, created_at: 2014-08-18 18:15:14 UTC, updated_at: 2014-08-18 18:15:14 UTC, text: "tag tag", group_id: "53f242b27f11ba5f31000001", _type: "TagTag">,
 #<GroupTag _id: 53f242b27f11ba5f31000004, created_at: 2014-08-18 18:15:14 UTC, updated_at: 2014-08-18 18:15:14 UTC, text: "group tag", group_id: "53f242b27f11ba5f31000001", _type: "GroupTag">]
collections: ["groups", "tag"]
Finished tests in 0.527855s, 3.7889 tests/s, 3.7889 assertions/s.
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips