在RDBMS教程中建立Neo4j.rb数据库



我对Rails和Neo4j都是新手。我一直在尝试使用Neo4j.rb,同时遵循团队树屋关于构建简单Ruby应用程序的教程。我意识到这两个数据模型集在语法上有很多差异,但似乎无法做到这一点。我查看了各种Neo4j.rb资源,但似乎找不出问题出在哪里。它似乎在html.erb文件中,但我不能确定它不是图形本身,因为Neo4j管理宝石不适用于我的jruby版本。

以下是我的模型文件示例,我的用户名为Knockers(长话短说!),我使用的是cfitz创建的devise-neo4j-gem。如果你注意到任何显著的低效,请让我也知道(肯定会有负载!):

class Knocker < Neo4j::Rails::Model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :knocker_id
property :first_name, :type => String
property :last_name, :type => String
property :facebook_token, :type => String, :index => :exact
property :created_at, :type => Time
property :updated_at, :type => Time
has_n(:statuses).from(Status, :created_by).relationship(Created_By)
property :email, :type => String, :null => false, :default => "", :index => :exact
property :encrypted_password, :type =>  NilClass
property :knocker_id, :type => Fixnum 
index :knocker_id
property :remember_created_at, :type => Time
index :remember_token, :type => :exact
property :reset_password_token,   :type => NilClass, :index => :exact
property :reset_password_sent_at, :type =>   Time
property :sign_in_count, :type => Fixnum, :default => 0
property :current_sign_in_at, :type => Time
property :last_sign_in_at, :type => Time
property :current_sign_in_ip, :type =>  String
property :last_sign_in_ip, :type => String
# property :confirmation_token, :type => NilClass, :index => :exact
# property :confirmed_at, :type => DateTime
# property :confirmation_sent_at, :type => DateTime
## Lockable
#  property :failed_attempts, :type => Fixnum, :default => 0
# property :locked_at, :type => DateTime
#  property :unlock_token, :type => String, :index => :exact
## Token authenticatable
# property :authentication_token, :type => String, :null => true, :index => :exact
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def full_name
first_name + " " + last_name
end
end
class Status < Neo4j::Rails::Model
attr_accessible :content, :knocker_id
property :content, :type => String
property :knocker_id, :type => Fixnum
property :created_at, :type => Time, :value => Time.now
has_one(:created_by).to(Knocker).relationship(Created_By)
validates :content, presence: true,
length: { minimum: 2 }
validates :knocker_id, presence: true
end
class Created_By < Neo4j::Rails::Relationship
property :created_at
def to_key
persisted? ? [id] : nil
end
# this is for the routing helpers
def to_param
persisted? ? neo_id.to_s : nil
end
end

以下是状态视图show.html.erb文件:

<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @status.incoming(Knocker.created_by) %>
<% end %>
</p>
<p>
<b>Content:</b>
<%= @status.content %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>

下面是状态控制器:

class StatusesController < ApplicationController
before_filter :authenticate_knocker!, only: [:new]
# GET /statuses
# GET /statuses.json
def index
@statuses = Status.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
@status = Status.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @status }
end
end
# GET /statuses/new
# GET /statuses/new.json
def new
@status = Status.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
@status = Status.find(params[:id])
end
# POST /statuses
# POST /statuses.json
def create
@knocker = Knocker.find(params[:knocker_id])
@status = Status.new(params[:status])
respond_to do |format|
if @status.save
format.html { redirect_to @status, notice: 'Status was successfully created.' }
format.json { render json: @status, status: :created, location: @status }
else
format.html { render action: "new" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# PUT /statuses/1
# PUT /statuses/1.json
def update
@status = Status.find(params[:id])
respond_to do |format|
if @status.update_attributes(params[:status])
format.html { redirect_to @status, notice: 'Status was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
def destroy
@status = Status.find(params[:id])
@status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
end

如前所述,我尝试了许多不同的方法(因此,这很可能是方法的混搭!)。起初,我只是在模型中添加了has_one和has_n属性,但在阅读Neo4j.rb文档时,我试图将其基于Actor、Movie、Role布局。。。似乎不起作用!服务器没有在show.html.erb页面上显示用户/Knocker的名称,并且当前显示错误:

undefined method `created_by' for Knocker:Class

问题很明显是因为我一直在学习一个基于RDBMS的教程。如果有人对如何轻松地将RDBMS应用程序转换为Neo4j.rb有任何有用的提示,那将非常方便!

为这个愚蠢的问题道歉,我显然直接错过了一些东西!

到目前为止,完整的文件可以在https://github.com/davemate23/knock4

此错误来自哪个视图?

我看到在statuses.html.erb中,您返回了一个节点的Enumerable,而不是一个属性,@status.incoming(Knocker.created_by)尝试在Status类上声明一个与has_one的关系,这样您就可以编写@status.created_by.name

或者使用核心api尝试@status.node(:outgoing, Knocker.statuses).name(请参阅https://github.com/andreasronge/neo4j/wiki/Neo4j%3A%3ACore-节点属性关系)

顺便说一句,这应该在控制器或模型层中完成。

另一个可能的错误是您在关系中添加了错误的节点。创建关系时没有验证。例如,some_user.status << User.create()不会抱怨,但当您在遍历状态关系时意外发现用户对象时,可能会出现错误。

最新更新