Kkulikovskis
Kkulikovskis

Reputation: 2088

find_by in rails with neo4j not working

This question implies 2 questions:

I have 2 models - User and SocialNetworkConnection

I want to create records of both objects, when a user authenticates through facebook. Creation of them and relation works fine.

Question 1: The problem is that find_by method is not working and instead creates 2 more records, even if the user and soc_network_con already exist.

If I try to use the find_by by inserting hard coded provider and uid it works.

I have checked what is the output of auth.provider and auth.uid and it is what it should be. I use this method in my controller by passing in request.env['omniauth.auth'].

Question 2: When A SocialNetworkConnection record is found I want to find the corresponding User node but the user_connection.user line doesn't seem to be working (tried it in irb).

user.rb

class User 
  include Neo4j::ActiveNode

  property :name, type: String
  property :email, type: String
  property :image_url, type: String

  has_many :in, :social_network_connections, origin: :user

end

social_network_connection.rb

class SocialNetworkConnection 
  include Neo4j::ActiveNode

  property :provider, type: String
  property :uid, type: Integer
  property :token, type: String
  property :expires_at, type: Integer

  has_one :out, :user, type: :connection_to

    def self.find_for_facebook_oauth(auth)
       user_connection = SocialNetworkConnection.find_by(provider: auth.provider, uid: auth.uid )
       if user_connection
        user = user_connection.user  
       else
         user_connection = SocialNetworkConnection.create!(provider:auth.provider,
                              uid:auth.uid,
                              token:auth.credentials.token,
                              expires_at:auth.credentials.expires_at
                              )
         user = User.create!(name:auth.info.name,
                            email:auth.info.email,
                            image_url:auth.info.image
                            )

        user.social_network_connections << user_connection

        end  

     return user

    end

end

Upvotes: 0

Views: 42

Answers (1)

Kkulikovskis
Kkulikovskis

Reputation: 2088

QUESTION 1: Since I stored auth.uid as an integer, but it is returned as a string I needed to add .to_i (so in the params I would have uid: auth.uid.to_i)

QUESTION 2: In the middle of looking for the problem in question 1 I changed the relationship type so When the 1st problem was solved, it always found the old relationship first (with the old type) so It could not find the corresponding user node.

Upvotes: 0

Related Questions