Creating relationships in a Neo4J model with after_save

Therefore, I apologize for how these problems may seem. I am new to rails, and as a first task I also brought to Neo4J, as it seemed best suited if I grow a project.

I will explain the flow of actions and then show an example of code. Now I'm trying to add step 3-5.

  • User is registered through FB
  • The first login creates the user node. If the user exists, he simply retrieves that user + node
  • After creating the node user, the koala stone is used to access the FB API
  • Gets a friend list of each friend using the app.
  • Go through each friend and add a two-way friendship between two users.

As soon as 3-5 should happen when the user first joins, I thought I could do this in a method involving a callback after_save. There is a flaw in this logic, although I will need to update the user with additional attributes at some point, and he will call after_save again. Can I prevent this from updating?

SessionController for reference

  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id  
    redirect_to root_url
  end

  def destroy
    session.delete(:user_id)
    redirect_to root_path
  end

So in my user.rb I have something like this

 has_many :both, :friendships

  after_save :check_friends


  def self.from_omniauth(auth)
    @user = User.where(auth.slice(:provider, :uid)).first

    unless @user
      @user = User.new
      # assign a bunch of attributes to @user

      @user.save!
    end
    return @user
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)

    block_given? ? yield(@facebook) : @facebook
      rescue Koala::Facebook::APIError => e
      logger.info e.to_s
      nil
  end

  def friends_count
    facebook { |fb| fb.get_connection("me", "friends", summary: {}) }
  end

  def check_friends(friendships)
    facebook.get_connection("me", "friends").each do |friend|
      friend_id = friend["id"]
      friend_node = User.where(friend_id)
      Friendship.create_friendship(user,friend_node)
      return true
    end
  end

friendship.rb

  from_class User
  to_class   User
  type 'friendship'

  def self.create_friendship(user,friend_node)
    friendship = Friendship.create(from_node: user, to_node: friend_node)
  end   

I'm not sure I'm on the right track how to create a node relationship. Once I have created @user, how can I incorporate this into my method check_friendsand get the node user and friend correctly so that I can link them together.

Now he does not know that the user and friend_user are nodes

If you see another bad code practice, please let me know!

@subvertallchris. , , .

+4
1

! , , , .

has_many. node, ActiveRel, - :

has_many :both, :friends, model_class: 'User', rel_class: 'Friendship'

.

, Neo4j. , , . FRIENDS_WITH .

, .

! , ! after_save / .

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    @user = user.nil? ? User.create_from_omniauth(env["omniauth.auth"]) : user
    session[:user_id] = @user.id
    redirect_to root_url
  end

  def destroy
    session.delete(:user_id)
    redirect_to root_path
  end
end


class User
  include Neo4j::ActiveNode
  # lots of other properties
  has_many :both, :friends, model_class: 'User', rel_class: 'Friendship'

  def self.from_omniauth(auth)
    User.where(auth.slice(:provider, :uid)).limit(1).first
  end

  def self.create_from_omniauth(auth)
    user = User.new
    # assign a bunch of attributes to user
    if user.save!
      user.check_friends
    else
      # raise an error -- your user was neither found nor created
    end
    user
  end

  # more stuff
end

, . , .

. check_friends:

def check_friends(friendships)
  facebook.get_connection("me", "friends").each do |friend|
    friend_id = friend["id"]
    friend_node = User.where(friend_id)
    Friendship.create_friendship(user,friend_node)
    return true
  end
end

, . , , node, find_by. , facebook_id.

def check_friends
  facebook.get_connection("me", "friends").each do |friend|
    friend_node = User.find_by(facebook_id: friend["id"])
    Friendship.create_friendship(user,friend_node) unless friend_node.blank?
  end
end

create_friendship true false, , , , . :

def self.create_friendship(user, friend_node)
  Friendship.new(from_node: user, to_node: friend_node).save
end

create true false, , save , . , .

after_create ActiveRel, - from_node, , . , . , ActiveRel .

, , . facebook . .

# models/concerns/facebook.rb

module Facebook
  extend ActiveSupport::Concern

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)

    block_given? ? yield(@facebook) : @facebook
      rescue Koala::Facebook::APIError => e
      logger.info e.to_s
      nil
  end

  def friends_count
    facebook { |fb| fb.get_connection("me", "friends", summary: {}) }
  end
end

# now back in User...

class User
  include Neo4j::ActiveNode
  include Facebook
  # more code...
end

. . !

. , - - , , , , . , .

+5

All Articles