简体   繁体   中英

Ruby on Rails filtering attributes on returning model object

I'm creating an API for a Rails app and I want to return User objects for an API call but without crypted_password , salt , or login_token attributes.

Is there any way to do something like this:

do api_fetch_user(u)
  user=User.find(u)
  return user(:filter=>"crypted_password", "salt", "login_token")
end
def api_fetch_user(user)
  user.attributes.select { |key,v| AVAILABLE_USER_FIELDS.include?(key) }
end

This might work in your case, i haven't tried that personally though. Try to reset the Filtered Attributes to nil before returning the Object.

do api_fetch_user(u)
   user=User.find(u)
   user.crypted_password = ''
   user.salt = ''
   user.login_token = ''
   return user
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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