How to make Users automatically Follow Admin User on Sign Up

戏子无情 提交于 2019-11-30 07:47:08

In user.rb add a after_create filter

after_create :follow_admin!

def follow_admin!
  relationships.create!(followed_id: admin_user.id)
end

In create action before sign_in add

@user.follow! admin_user

you need to fetch admin user first somehow. Good idea would be to make follow!, following? and unfollow! methods to accept either id or object as in

def follow!(user_or_id)
  id = (user_or_id.is_a?(User) ? user_or_id.id : user_or_id)
  relationships.create!(followed_id: id)
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!