如何为每个resource_owner_id和范围组合保留一个记录(最后/最新),并删除其他记录?
例如,对于resource_owner_id = 3和scopes = driver,它应该仅记录id为1357的记录。resource_owner_id= 5和scopes = driver-ID为1034的记录
我知道我可以使用function fb_signup(){
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function success(userData){
var uid = userData.uid;
var displayName = userData.displayName;
var email = userData.email;
var emailVerified = userData.emailVerified;
var photoURL = userData.photoURL;
var isAnonymous = userData.isAnonymous;
var providerData = userData.providerData;
$('#fbFormMessage').css({'display':'block','background-color':'#c9e9cc','color':'#347f3b'});
document.getElementById("fbFormMessage").innerHTML = 'Welcome Your registration was successful.';
console.log(uid + ' | ' + email + ' | ' + uName )
saveNewUser(uid, email, uName);
return false;
})
.catch(function failure(error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode + " " + errorMessage);
$('#fbFormMessage').css({'display':'block','background-color':'#FFEBEE','color':'#EF5350'});
document.getElementById("fbFormMessage").innerHTML = 'The email address is already in use by another account';
});
};
function saveNewUser(a,b,c){
console.log('4')
var rootRef = firebase.database().ref();
var storesRef = rootRef.child('/users/');
var newStoreRef = storesRef.push();
newStoreRef.set({
uid: a,
userName: c,
email: b,
});
};
,然后按ID和范围(作用域为枚举-'driver'和'passenger')获取所有记录,之后使用.ids
并删除所有剩余的记录记录。
也许有一个更优雅的解决方案?
答案 0 :(得分:0)
ActiveRecord版本:
Model.select("DISTINCT ON(resource_owner_id, scopes) *")
.order("resource_owner_id, scopes, id")
详细了解DISTINCT ON
here。