ruby datamapper - 如何保存查询

发布于 2025-01-06 02:25:19 字数 272 浏览 3 评论 0原文

我有一个模型用户,其属性为姓名、年龄、性别。用户可以使用以下条件查询年龄和性别。如何保存此查询。一种方法是将其转换为age =>年龄 1 和年龄 2 之间以及性别 => 'M' 并将此查询保存在包含用户 ID、查询字符串和查询名称的表中。 这样做的好方法是什么..如果我必须使用这个查询来找出将来还有谁适合这个角色(比如说其他人会保存一个查询,我需要找到我适合谁的查询)关于我的年龄和性别..基本上我需要能够将我的年龄和性别与保存的查询/选择标准相匹配) 我正在使用 sinatra 作为模型,

谢谢

I have a model user with name,age,gender as attributes. a user can query with the following criteria age and gender. how to save this query. one way would be to convert it as age => between age1 and age2 and gender => 'M' and save this query in a table with user id and query string and a name for the query.
what would be good way to do this..if i have to use this query to find out who else would fit in this role in future(say somebody else would have saved a query and i need to find whose queries would I fit in based on my age and gender ..basically i need to be able to match my age and gender to the saved query/ selection criteria)
I am using sinatra for the model

thanks

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

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

发布评论

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

评论(1

撩动你心 2025-01-13 02:25:19

我喜欢 DataMapper,但在某些情况下,最好只编写一些 SQL 来解决您的问题,然后让 DataMapper 为您完成部分工作。我通常使用 DataMapper 进行相对简单的查询,而当事情变得更复杂时,我会使用旧的 SQL。

在您的情况下,SQL 命令将如下所示:

SELECT * FROM people
WHERE (want_age_min IS NULL OR age >= want_age_min)
AND (want_age max IS NULL OR age <= want_age_max)
AND (want_sex IS NULL OR want_sex = my_sex)

您从已在系统中注册的所有人员中进行选择,并为每个相关属性添加一个条件。结果将是人们要么不关心最低年龄(want_age_min 为空),要么最低年龄小于或等于候选人的年龄。所有属性都相同。

这样,“保存的查询”只是一行,其中包含指定条件的列。然后您实际上可以对所有情况使用这一查询。

I like DataMapper, but there are cases when it's better to just write some SQL to solve your problem and then let DataMapper do part of the work for you. I usually use DataMapper for the relatively simple queries and resort to good old SQL when things get more complicated.

In your case, the SQL command would look like this:

SELECT * FROM people
WHERE (want_age_min IS NULL OR age >= want_age_min)
AND (want_age max IS NULL OR age <= want_age_max)
AND (want_sex IS NULL OR want_sex = my_sex)

You're selecting from all people already enrolled in the system, and you're adding one criterion for each relevant attribute. The result will be people who either don't care about minimum age (want_age_min is null) or the minimum age is less than or equal to the candidate's age. And the same for all attributes.

This way, the "saved query" is just a row with columns specifying conditions. Then you can actually use this one query for all cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文