Infinity、ActiveRecord 和 MySQL:存储和比较
我有用户输入字符串指定事件的费用(例如:“非会员 4 美元”)。 我正在尝试解析成本的上限(和下限)(在示例中,upper == lower == 4.00
)。
假设给定的字符串无法解析(可能是空白)。 在这种情况下,我希望能够在数据库中存储事件的最大成本为无穷大(最小为 0)。 如何使用 Mysql 和 ActiveRecord 执行此操作?
我尝试过的:
> e = Event.find(1234)
> e.cost_max = 1.0/0.0
> e.save
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'Infinity' in 'field list': UPDATE `events` SET `cost_max` = Infinity WHERE `id` = 1234
我也尝试用 NULL 标记 cost_max 。 这原则上是有效的。 但是,我使用 Ultrasphinx 过滤器来检索成本范围与搜索范围有非空交集的事件。 由于 Ultrasphinx 过滤器只接受哈希条件(我错了吗?),我的选项仅限于此:
search_params[:filter].merge!(:cost_max => 0.0..99)
意思是,不允许“OR cost_max is NULL”。
任何想法?
I have user input strings specifying the cost of an event (eg: "$4 for non-members"). I am trying to parse out the upper (and lower) bound of the cost (in the example, upper == lower == 4.00
).
Suppose that a given string cannot be parsed (maybe it's blank). In this case, I want to be able to store in the database that the maximum cost of the event is infinity (and minimum 0). How do I do this using Mysql and ActiveRecord?
What I've tried:
> e = Event.find(1234)
> e.cost_max = 1.0/0.0
> e.save
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'Infinity' in 'field list': UPDATE `events` SET `cost_max` = Infinity WHERE `id` = 1234
I've also tried marking cost_max with NULL, instead. This works in principle. However, I am using Ultrasphinx filters to retrieve events whose cost range has a non-empty intersection with a searched range. As Ultrasphinx filters accept only hash conditions (am I wrong?), my options are limited to this:
search_params[:filter].merge!(:cost_max => 0.0..99)
Meaning, no "OR cost_max is NULL" allowed.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果字段类型是 double ,那么您似乎可以存储无穷大的值。
然后只需将其用作比较的上限即可。
有关详细信息,请参阅 mysql 邮件列表中的此线程。
If the field type is a
double
it seems that you can store infinity values.Then it's just a matter of using that as the upper bounds for the comparison.
For more information see this thread in the mysql mailing lists.