繁体   English   中英

在neo4j无法正常运行的rails中查找

[英]find_by in rails with neo4j not working

这个问题意味着两个问题:

我有2个模型 - User和SocialNetworkConnection

当用户通过facebook进行身份验证时,我想创建两个对象的记录。 创建它们和关系很好。

问题1:问题是find_by方法不起作用,而是创建了2条记录,即使用户和soc_network_con已经存在。

如果我尝试通过插入硬编码提供程序和uid来使用find_by,它就可以工作。

我已经检查了auth.provider和auth.uid的输出是什么,它应该是什么。 我通过传入request.env ['omniauth.auth']在我的控制器中使用此方法。

问题2:当找到SocialNetworkConnection记录时,我想找到相应的用户节点,但user_connection.user行似乎不起作用(在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

问题1:因为我将auth.uid存储为整数,但是它作为字符串返回我需要添加.to_i(因此在params中我会有uid:auth.uid.to_i)

问题2:在寻找问题1中的问题的过程中,我更改了关系类型,因此当第一个问题解决后,它始终首先找到旧关系(使用旧类型),因此无法找到相应的用户节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM