逐步或迭代大事
我的起点基本上是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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能转换 bigdecimal
“ http://ruby-doc.org/core/classes/range.html#m000702” rel =“ nofollow”>精细手册:
并且BigDecimal没有
scucs
方法时,您才能迭代。当您对此进行喂食时:将ActivereCord作为条件,可能会发现您已经给了它一个范围并在其上调用
to_a
以获取可以是SQL-IFIED的东西,这就是您的错误来自。我认为您想这么说:
类似的问题适用于您的
hour_price_high
。即使
price_low
和price_high
是整数,您也不想在中使用,范围可以扩展到相当大的列表,数据库将必须单独比较每个。
You can't convert a Range of BigDecimal to an array:
From the fine manual:
And BigDecimal doesn't have a
succ
method. When you feed this: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:
Similar issues apply to your
hour_price_high
.Even if
price_low
andprice_high
were integers you wouldn't want to useIN
, the range could expand to a rather large list and the database would have to compare each one individually.