ruby on rails - Neo4J Gem - Saving undeclared relationships -
i trying create realtionship between 2 nodes described here https://github.com/neo4jrb/neo4j/wiki/neo4j-v3-declared-relationships from_node.create_rel("friends", to_node)
i getting undefined method create_rel
what doing wrong? trying create q+a system inside model. both questions , answers treated models right now.
i'm getting undefined method
create_rel' #
event.rb
has_many :out, :event_questions
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
has_one :in, :event_questions
event_questions_controller.rb
def new #is 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
i have new question sitting inside event's index page since wanted list questions on event after. don't need new
method in controller right? don't know how obtain event question form sitting on. accessible through params?
update
did mean this
def create_questions_of(to_node) self.create_rel("questions_of", to_node) end
and
@event_question.create_questions_of(@event)
so think need change routes , nest questions inside create events/123/questions/
then can grab events_id
, use find
update #2
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
has_one :in, :events, origin: :event_questions
events/show.html.erb
<%= form_for [:event, @event_question] |f| %> #form stuff <% end %>
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
resources :events resources :event_questions, only: [:create, :destroy] end
create_rel
worked fine when tested now. saying undefined method 'create_rel' nil:nilclass
? if so, means from_node
variable doesn't have node set. make sure objects think are.
the better question here: why want this? when create undeclared relationship, have write own cypher queries whenever want use it. if it's part of code , using regularly, should have has_many
associations in models. create_rel
exists provide interoperability nodes don't have models.
as other question, don't need new
action unless there's route , view corresponds it. if you're loading form new question on index page, that's fine. if url http://127.0.0.1:3000/events/123/questions/
, can event id in params[:event_id]
. run rake routes
command project's directory , it'll spit out lots of information includes parameter names.
finally, when use self
in @event_question.create_questions_of(self,@event)
, you're going controller. if want refer @event_question
, remove first argument create_questions_of
, use self
within method.
edit: part 2
you're getting undefined method
because self
in @event_question.create_questions_of(self,@event)
controller. you're trying send @event_question itself, think. don't that, call self
within create_questions_of
, you'll current eventquestion.
you use activerel if want callbacks, validations, properties, etc,... if want simple relationships, setup has_many
associations in each model, omit rel_class
, , either set them both 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
says, "look association in reciprocal model , use type defines." lets not have worry synchronizing type between associations.
after that, can @event_question.events << @event
, it'll create new relationship you.
Comments
Post a Comment