Rails Associated Table Does not update -
i have 2 models
influencers(id, first_name, ....) influencer_authorizations(id, provider, provider_uid, oauth_token, ....)
influencers has_many influencer_authorizations
i trying login or sign influencer
# 1. check if token valid # 2. check if influencer has authorization facebook_uid # 3. if no - create new influencer # 4. if yes - loging in. update token , log him in
but when old user tries login oauth token not getting updated
facebook controller
def create @facebook_login = facebookclient.new(facebook_user_token) if @facebook_login.create render :show else render :error end end
facebook client
class facebookclient def initialize(token) @token = token @client = koala::facebook::api.new(token) end attr_reader :influencer def create @influencer = new_or_existing_influencer save_influencer end def errors { facebook_error: @facebook_errors } end private attr_reader :client def new_or_existing_influencer influencer = influencer.joins(:influencer_authorizations).where( :influencer_authorizations => { provider: 'facebook', provider_uid: provider_uid } ).first_or_initialize influencer end def save_influencer set_influencer_details if @influencer.new_record? set_influencer_authorization_details if @influencer.new_record? @influencer.influencer_authorizations[0].oauth_token = @token if !influencer.new_record? @influencer.save end def set_influencer_details @influencer.assign_attributes( first_name: first_name, last_name: last_name, email: email, bio: bio, date_of_birth: birthday, gender: gender, location: location ) end def set_influencer_authorization_details @influencer.influencer_authorizations.build( provider: 'facebook', provider_uid: provider_uid, oauth_token: @token, social_account: socialaccount.friendly.find('facebook') ) end def basic_information begin @basic_information ||= client.get_object("me?fields=id,first_name,last_name,name,bio,about,birthday,email,timezone,gender,location,hometown,currency,locale,devices") rescue koala::facebook::apierror => exc @facebook_errors = exc.message end end def provider_uid basic_information["id"] end def first_name basic_information["first_name"] end def last_name basic_information["last_name"] end def name basic_information["name"] end def email basic_information["email"] end def bio basic_information["bio"] end def location basic_information["location"].present? ? basic_information["location"]["name"] : "" end def birthday basic_information["birthday"].present? ? date.strptime(basic_information["birthday"], "%m/%d/%y") : "" end def gender basic_information["gender"].present? ? basic_information["gender"] : "not_specified" end end
just before saving on inspect oauth token set @influencer.influencer_authorizations[0].oauth_token
Comments
Post a Comment