逐步或迭代大事

发布于 2024-12-05 06:39:29 字数 990 浏览 1 评论 0原文

我的起点基本上是Ryan Bate的 railscast < /a>。我已经修改了他的代码以适应或条款。

在用户模型中,有几个属性名为hour_price_high和hour_price_low。用户将设置其小时费率范围,例如$ 45.25/h,至$ 55.50/h。现场访问者将在价格范围内搜索注册用户,例如$ 50.00/h至$ 60.00/h。此搜索应返回所有重叠的用户。这些属性的各个迁移条目是:

# CreateUser migration
...
t.decimal "hour_price_high", :precision => 6, :scale => 2
t.decimal "hour_price_low",  :precision => 6, :scale => 2
...

它们是类型的bigdecimal,我需要通过这些迁移来搜索用户:

# Search.rb
def low_price_or_conditions
    ["users.hour_price_low IN (?)", price_low..price_high] unless price.blank?
end
def high_price_or_conditions
    ["users.hour_price_high IN (?)", price_low..price_high] unless price.blank?
end

当我运行此代码时,我会收到此错误:

TypeError:
can't iterate from BigDecimal

关于如何通过这些递增的想法?我只需要递减两个小数点。感谢您的查看!

My starting point is basically Ryan Bate's RailsCast. I have modified his code to accomodate OR clauses as well as AND.

In the User model there are a couple of attributes named hour_price_high and hour_price_low. Users will set their hourly rate range such as $45.25/h to $55.50/h. Site visitors will search for registered users in a price range such as $50.00/h to $60.00/h. This search should return all overlapping users. The respective migration entries for these attributes are:

# CreateUser migration
...
t.decimal "hour_price_high", :precision => 6, :scale => 2
t.decimal "hour_price_low",  :precision => 6, :scale => 2
...

They are of type BigDecimal and I need to increment through these to search for users:

# Search.rb
def low_price_or_conditions
    ["users.hour_price_low IN (?)", price_low..price_high] unless price.blank?
end
def high_price_or_conditions
    ["users.hour_price_high IN (?)", price_low..price_high] unless price.blank?
end

When I run this code, I get this error:

TypeError:
can't iterate from BigDecimal

Any ideas of how to increment through these? I only need to increment back two decimal places. Thanks for looking!

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

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

发布评论

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

评论(1

爱人如己 2024-12-12 06:39:29

您不能转换 bigdecimal

> a = BigDecimal.new('1')
 => #<BigDecimal:12a082bb0,'0.1E1',9(18)> 
> b = BigDecimal.new('5')
 => #<BigDecimal:12a076ec8,'0.5E1',9(18)> 
> (a..b).to_a
TypeError: can't iterate from BigDecimal

“ http://ruby-doc.org/core/classes/range.html#m000702” rel =“ nofollow”>精细手册:

您只有在范围的启动对象支持Succ方法

的情况下才能迭代

并且BigDecimal没有scucs方法时,您才能迭代。当您对此进行喂食时:

["users.hour_price_low IN (?)", price_low..price_high]

将ActivereCord作为条件,可能会发现您已经给了它一个范围并在其上调用to_a以获取可以是SQL-IFIED的东西,这就是您的错误来自。

我认为您想这么说:

[ "users.hour_price_low >= :lo AND users.hour_price_low <= :hi", { :lo => price_low, :hi => price_hi } ]

类似的问题适用于您的hour_price_high

即使price_lowprice_high是整数,您也不想在中使用,范围可以扩展到相当大的列表,数据库将必须单独比较每个。

You can't convert a Range of BigDecimal to an array:

> a = BigDecimal.new('1')
 => #<BigDecimal:12a082bb0,'0.1E1',9(18)> 
> b = BigDecimal.new('5')
 => #<BigDecimal:12a076ec8,'0.5E1',9(18)> 
> (a..b).to_a
TypeError: can't iterate from BigDecimal

From the fine manual:

You can only iterate if the start object of the range supports the succ method

And BigDecimal doesn't have a succ method. When you feed this:

["users.hour_price_low IN (?)", price_low..price_high]

to ActiveRecord as a condition, it probably sees that you've given it a Range and calls to_a on it to get something that can be SQL-ified and that's where your error comes from.

I think you want to say this:

[ "users.hour_price_low >= :lo AND users.hour_price_low <= :hi", { :lo => price_low, :hi => price_hi } ]

Similar issues apply to your hour_price_high.

Even if price_low and price_high were integers you wouldn't want to use IN, the range could expand to a rather large list and the database would have to compare each one individually.

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