【问题标题】:Referencing the same model 3 times from another model in Mongoid从 Mongoid 中的另一个模型引用相同模型 3 次
【发布时间】:2014-08-01 19:57:37
【问题描述】:

我有两个模型如下:

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 种不同类型的 Group 分别可引用。任何帮助将不胜感激!

【问题讨论】:

    标签: ruby activerecord mongoid mongoid3


    【解决方案1】:

    我认为以下内容可以满足您的需求,尽管还有三个额外的琐碎模型/子类。 我尝试仅使用 Tag 类并且没有子类遇到冲突和其他问题 与反比关系,即使在放松多态性限制时也是如此。 也许这三个额外的琐碎子类可以满足您的目的? 请注意,标签数据仍存储在“store_in”方法指定的一个“标签”集合中。

    app/models/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
    

    app/models/tag.rb

    class Tag
      include Mongoid::Document
      include Mongoid::Timestamps
      field :text, type: String
    
      belongs_to :group
      store_in collection: 'tag'
    end
    

    app/models/group_tag.rb

    class GroupTag < Tag
    end
    

    app/models/tag_tag.r

    class TagTag < Tag
    end
    

    app/models/user_tag.rb

    class UserTag < Tag
    end
    

    test/unit/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
    

    耙式测试

    运行选项:

    运行测试:

    [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
    

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 2013-08-12
      • 2011-05-13
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多