Neo4J Gem-保存未声明的关系 [英] Neo4J Gem - Saving undeclared relationships

查看:86
本文介绍了Neo4J Gem-保存未声明的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个节点之间创建一个Realshipship,如此处所述 https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3声明的关系 from_node.create_rel("FRIENDS", to_node)

I am trying to create a realtionship between two nodes as described here https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3-Declared-Relationships from_node.create_rel("FRIENDS", to_node)

我正在得到create_rel

我做错了什么?我正在尝试在另一个模型中创建一个Q + A系统.因此,问题"和答案"现在都被视为模型.

What am I doing wrong? I am trying to create a Q+A system inside another model. So both Questions and Answers are treated as models right now.

我正在为#获取一个undefined method create_rel'

event.rb

I'm getting a undefined methodcreate_rel' for #

event.rb

  has_many :out, :event_questions

event_question.rb

event_question.rb

  has_one :in, :events
  has_many :out, :event_answers

  def create_questions_of(from_node,to_node)
    from_node.create_rel("questions_of", to_node)
  end

event_answer.rb

event_answer.rb

  has_one :in, :event_questions

event_questions_controller.rb

event_questions_controller.rb

def new
    #is this needed
end

def create
    @event_question = EventQuestion.new(event_question_params)
    if @event_question.save
        @event = Event.find(params[:id])
        @event_question.update(admin: current_user.facebook_id)
        @event_question.create_questions_of(self,@event)
        redirect_to @event
    else
        redirect_to @event
    end
end

private

    def event_question_params
        params.require(:event_question).permit(:question)
    end

我想在事件的索引页面中放置新问题,因为我想在此之后列出事件中的所有问题.我什至不需要控制器中的new方法,对吗?我也真的不知道我该如何获得我的问题表格.通过参数可以访问吗?

I have my new question sitting inside the event's index page since I wanted to list all the questions on the event after. I don't even need a new method in my controller right? I also don't really know how I would obtain the event that my question form is sitting on. Is that accessible through params?

更新

您是不是这个意思

  def create_questions_of(to_node)
    self.create_rel("questions_of", to_node)
  end

 @event_question.create_questions_of(@event)

所以我认为我也需要更改路线,并在其中嵌套问题以创建 events/123/questions/

So I think I need to change my routes as well and nest questions inside to create events/123/questions/

然后我可以抓住events_id并使用find

Then I can grab events_id and use find

更新#2

events_controller.rb

events_controller.rb

def show
    @event = Event.find(params[:id])
    @event_question = EventQuestion.new
end

event.rb

  has_many :out, :event_questions, type: 'questions_of'

event_question.rb

event_question.rb

  has_one :in, :events, origin: :event_questions

events/show.html.erb

events/show.html.erb

<%= form_for [:event, @event_question] do |f| %>

#form stuff

<% end %>

event_questions_controller.rb

event_questions_controller.rb

def create
    @event_question = EventQuestion.new(event_question_params)
    if @event_question.save
        @event = Event.find(params[:event_id])
        @event_question.update(admin: current_user.facebook_id)
        @event_question.events << @event
        redirect_to @event
    else
        redirect_to :back
    end
end

routes.rb

routes.rb

resources :events do
    resources :event_questions, only: [:create, :destroy]
  end

推荐答案

create_rel在我刚刚进行测试时工作正常.是说undefined method 'create_rel' for nil:NilClass吗?如果是这样,则意味着您的from_node变量实际上没有设置节点.确保您的对象就是您认为的样子.

create_rel worked fine when I tested it just now. Is it saying undefined method 'create_rel' for nil:NilClass? If so, it means that your from_node variable doesn't actually have a node set. Make sure your objects are what you think they are.

更好的问题是:为什么要这样做?创建未声明的关系时,无论何时要使用它,都必须编写自己的Cypher查询.如果它是代码的一部分,并且您定期使用它,则它在模型中可能应该具有has_many关联. create_rel实际上仅是为了提供与没有模型的节点的互操作性.

The better question here: why do you want to do this? When you create an undeclared relationship, you have to write your own Cypher queries whenever you want to use it. If it's part of your code and you are using it regularly, it should probably have has_many associations in your models. create_rel really only exists to provide interoperability with nodes that don't have models.

对于其他问题,除非有路线和与其对应的视图,否则您不需要new动作.如果您要在索引页面上加载新问题的表单,那很好.如果您的URL类似于http://127.0.0.1:3000/events/123/questions/,则可以在params[:event_id]中获取事件ID.从项目目录中运行rake routes命令,它将吐出很多信息,包括参数名称.

As for your other question, you don't need a new action unless there's a route and a view that corresponds with it. If you're loading the form for a new question on your index page, that's fine. If your URL is something like http://127.0.0.1:3000/events/123/questions/, then you can get the Event ID in params[:event_id]. Run the rake routes command from your project's directory and it'll spit out lots of information that includes the parameter names.

最后,当您在@event_question.create_questions_of(self,@event)中使用self时,将获得控制器.如果希望它引用@event_question,只需从create_questions_of中删除第一个参数,然后在方法内使用self.

Finally, when you use self in @event_question.create_questions_of(self,@event), you're going to get the controller. If you want it to refer to the @event_question, just remove that first argument from create_questions_of and use self from within the method.

第2部分

您正在获取undefined method,因为@event_question.create_questions_of(self,@event)中的self是控制器.我想,您正在尝试将@event_question发送给自己.不要这样做,只需从create_questions_of内部调用self,您将获得当前的EventQuestion.

You're getting the undefined method because self in @event_question.create_questions_of(self,@event) is the controller. You're trying to send @event_question to itself, I think. Don't do that, just call self from within create_questions_of and you'll get current EventQuestion.

如果需要回调,验证,属性等,请使用ActiveRel....如果只需要简单的关系,只需在每个模型中设置has_many关联,省略rel_class,然后将它们都设置为相同的type或将origin设置为一个.

You use ActiveRel if you want callbacks, validations, properties, etc,... If you just want a simple relationships, just setup the has_many associations in each model, omit rel_class, and either set them both to the same type or set origin on one.

class Event
  include Neo4j::ActiveNode
  has_many :in, :event_questions, type: 'questions_of'
end

class EventQuestion
  include Neo4j::ActiveNode
  has_many :out, :events, origin: :event_questions
end

origin说,在对等模型中查找此关联,并使用其定义的类型."它使您不必担心在关联之间进行类型同步.

origin says, "Look for this association in the reciprocal model and use the type it defines." It lets you not have to worry about synchronizing the type between associations.

之后,您可以执行@event_question.events << @event,它将为您创建新的关系.

After that, you can do @event_question.events << @event and it'll create a new relationship for you.

这篇关于Neo4J Gem-保存未声明的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆