红宝石与Datamapper 检查记录是否存在以及在哪里?

发布于 2024-08-11 03:56:17 字数 657 浏览 3 评论 0原文

我有一个使用 Sinatra、Datamapper 构建的基本 Ruby 应用程序,并使用 OAuth 进行用户身份验证。当我收到从 Oauth 服务返回的数据时,我将新用户的记录保存在 sqlite3 数据库中。

我不知道该怎么做是如何验证用户数据库表中不存在用户记录。我可以使用用户的唯一 id (uid) 来交叉检查该 uid 是否已存储,但我只是不确定在哪里执行此操作。

我有 2 个课程和一个 /callback 路线。 User 类是数据库模型,Authentication 类具有用于连接到 OAuth 的各种方法,以及 /callback 路由正在调用 Authentication.save 方法。

我应该在 Authentication.save 方法中检查现有记录并返回布尔值或其他值吗?在 Authentication 中创建一个类似于 Authentication.exists 的新方法? (那会是什么样子?)或者我应该在 /callback 路线中进行检查?

如果这不是 100% 清楚,我深表歉意,我很难描述我的问题,而且我是一个绝对的 Ruby 初学者......

I have a basic Ruby app that I am building with Sinatra, Datamapper and has user authentication using OAuth. When I receive the data back from the Oauth service, I save a record of a new user in an sqlite3 db.

What I don't know how to do is how to go about verifying the user record doesn't already exist on the user database table. I can use the user's unique id (uid) to cross check whether the uid is already stored, but I am just unsure where to do this.

I have 2 classes and a /callback route. The User class is the db model, and an Authentication class has assorted methods for connecting to the OAuth, and the /callback route which will have the Authentication.save method being called.

Should I be checking for an existing record within the Authentication.save method and return a boolean or something else? Create a new method in Authentication that would be like Authentication.exists? (and what would that look like?) Or should I be checking within the /callback route?

I apologize if this wasn't 100% clear, I am having a difficult time describing my issue and am an absolute Ruby beginner...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情 2024-08-18 03:56:17

在创建新用户之前,您应该尝试检查是否存在具有相同登录名和电子邮件(例如)的用户:

require 'dm-aggregates'

user=User.new
user.login=your_login_data_returned_by_oauth
user.email=your_email_data_returned_by_oauth
# And so on...
user.save if User.count(:login=>user.login, :email=>user.email) == 0

Just before creating your new user, you should try to check if an user with the same login and email (for example) exists :

require 'dm-aggregates'

user=User.new
user.login=your_login_data_returned_by_oauth
user.email=your_email_data_returned_by_oauth
# And so on...
user.save if User.count(:login=>user.login, :email=>user.email) == 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文