我一直在尝试为我网站上有FB的朋友的每个用户实施自动关注功能。当朋友列表中的某人注册到我的网站时,新用户应该自动关注我网站上的人和他/她的朋友列表。
当我尝试在网站上注册第一个用户时,标题中出现错误。我该如何解决这个问题?请检查我的自动跟踪代码是否良好。
我也在使用G +进行身份验证。
这就是我现在在user.rb中的内容:
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
after_create :follow_fbfriends!
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.image = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
#gets uid of user's fb friends
def fbfriends
@graph = Koala::Facebook::API.new(oauth_token)
begin
@fbfriends = @graph.get_connections("me", "friends", fields: "id")
@uids = @fbfriends.map{ |v| v.values }.flatten
rescue Koala::Facebook::AuthenticationError => e
redirect_to '/auth/facebook'
end
@friends = User.where(uid: @uids)
end
#User ID on my site(based on UID)
def fb_user_id
self.fbfriends.map{ |v| v.id }
end
#This should automatically follow user's fb friends if any of them are on my site
def follow_fbfriends!
relationships.create!(followed_id: fb_user_id)
end
relationship.rb
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
# validates :follower_id, presence: true
# validates :followed_id, presence: true
end