This goes back to my issue with this Neo4J Gem - Saving undeclared relationships
In the documentation, they give an example of this
post.comments = [comment1, comment2] # Removes all existing relationships
post.comments << comment3 # Creates new relationship
From what I understand, the first line would remove all relationships because it's manually setting all the associated comments to be comment1, comment2.
But what does line 2 do? post.comments should be retrieving the comments associated with post. Thus it seems to be creating a relationship with comment3 and the other comments. But don't we want to create a relationship between Posts and comments?
Either I'm not understanding the syntax and what it's doing or what is written isn't clear what is happening.
Looking at my example, we have
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
If event is my event node and @event_question is my event_question node, why does @event_question.events << event create the relationship between my event_question and event?
Right now I'm getting an undefined method '<<' for nil:NilClass in my controller which is a another problem altogether.
My event.rb has
has_many :in, :event_questions, type: 'questions_of'
and my event_question.rb has
has_one :out, :events, origin: :event_questions
@event_question and event both exist but @event_question.events will return nil
post.comments << comment3creates an additional relationship betweenpostandcomment3. It doesn't do anything with the other relationships.The reason this looks weird to you is because
post.commentsdoesn't retrieve anything from the database. Callclasson it or anyhas_manyassociation and you'll see that it's aNeo4j::ActiveNode::Query::QueryProxyobject, which you can think of as an unexecuted query. It doesn't actually go out and touch the database until you call an enumerable method likefirstoreachOR a custom operator like=or<<.<<is defined in query_proxy.rb.The lazy evaluation of QueryProxy objects is why you're able chain methods and do things like
post.comments.liked_by.username. It waits until it's clear that you need something from the database before hitting it.The reason you're getting
undefined method '<<' for nil:NilClassis becausehas_onerelationships work differently. They are not executed lazily because there's nothing to filter, there's nothing to chain, there's only one possible result. As soon as you call@event_question.events, it hits the database and since nothing is set, it returnsnil. The error means what it says:nildoes not have a<<method. Use@event_question.events = eventand you'll be fine. You may want to change that association tohas_one :out, :event, origin: :event_questionsto avoid confusion in the future.