我对Neoid
如何实现与neo4j
gem本身相比的关系感到有些困惑。
在Neo4j中,我们通过has_n
和has_one
指令在节点声明中指定关系(a.k.a edges):
class Person
include Neo4j::NodeMixin
property :name, index: :exact
property :city
has_n :friends
has_one :address
end
没有关于这些关系的另一端所属的类的明确声明,只要它是node
。
但Neoid
是另一回事。您必须在ActiveRecord
模型中指定关系,该模型通常是连接表/模型,如Neoid
主页中的示例所示:
用户:
class User < ActiveRecord::Base
include Neoid::Node
has_many :likes
has_many :movies, through: :likes
neoidable do |c|
c.field :slug
c.field :display_name
end
end
电影:
class Movie < ActiveRecord::Base
include Neoid::Node
has_many :likes
has_many :users, through: :likes
neoidable do |c|
c.field :slug
c.field :name
end
end
像:
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :movie
include Neoid::Relationship
neoidable do |c|
c.relationship start_node: :user, end_node: :movie, type: :likes
end
end
如您所见,为了指定Movie
和User
之间的关系,连接模型(Like
)已进行干预(映射到图数据库中的边缘) 。与嵌入式neo4j
API不同,在此示例中,边缘端点的类(:user
和:movie
)有明确的声明。
话虽这么说,我可以陈述我的情况:我有我的SQL数据库设置,我想为程序添加某种建议/兼容性功能。鉴于我申请的现状,Neoid
似乎很有希望。但我有一个疑问,我希望你能说清楚。
是否有可能使用活动记录多态选项来调整Neiod
中边缘端点的抽象性质?如:
class User < ActiveRecord::Base
include Neoid::Node
has_many :likes,
has_many :books, through: :likes
has_many :movies, through: :likes
has_many :sports, through: :likes
neoidable do |c|
c.field :name
end
end
class Book < ActiveRecord::Base
include Neoid::Node
has_many :likes, as: :likable
has_many :users, through: :likes
neoidable do |c|
c.field :title
c.field :author
end
end
#same setup for movies and sports
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :likable, polymorphic: true
include Neoid::Relationship
neoidable do |c|
c.relationship start_node: :user, end_node: :likable, type: :likes
end
end
此代码是否有效,或者我的错误?我需要改变方法吗? 我想在弄乱我的代码之前确定一下。任何见解将不胜感激......