Referencia del mismo modelo 3 veces desde otro modelo en Mongoide
-
02-01-2020 - |
Pregunta
Tengo dos modelos de la siguiente manera:
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
Pero esto y otros intentos no han parecido ceder el resultado deseado de tener 3 tipos diferentes de grupo de forma separada.Cualquier ayuda sería muy apreciada!
Solución
Creo que lo siguiente hace lo que quiere, aunque con tres modelos / subclases triviales adicionales. Mis intentos de hacer esto con solo la clase de etiquetas y ninguna subclases se ejecutó en colisiones y otros problemas con la relación inversa, incluso al aflojar las restricciones con el polimorfismo. Tal vez las tres subclases triviales adicionales están bien para sus propósitos? Tenga en cuenta que los datos de la etiqueta aún se almacenan en la colección de una 'etiqueta' como se especifica el método 'Store_in'.
aplicación / modelos / 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
aplicación / modelos / tag.rb
class Tag
include Mongoid::Document
include Mongoid::Timestamps
field :text, type: String
belongs_to :group
store_in collection: 'tag'
end
APLICACIÓN / MODELOS / GROUP_TAG.RB
class GroupTag < Tag
end
APP / MODELOS / TAG_TAG.R
class TagTag < Tag
end
aplicación / modelos / user_tag.rb
class UserTag < Tag
end
Prueba / unidad / 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
Prueba de rastrillo
Opciones de ejecución:
Pruebas de ejecución:
[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