I've been building a system that tracks royalties for authors. Say an author's royalty rate is based on sales: 0 - 5000 and they get say, 10 percent. 5001 - 10,000 they get 15 percent. My question, is how to I tell Rails that the final rule should be 10,001 - infinity? Right now, this is represented in a royalty_rules table with lower and upper columns and each royalty_rule belongs_to: contract and each contract :has_many royalty_rules.
I have a similar issue with dates ... I put beginning and end times in the dates, and sometimes, I don'开发者_运维知识库t want the contract to have an end date.
Although Ruby doesn't have constants for Infinity
or -Infinity
, it does know about the concepts and you can create them yourself:
irb> Numeric::Infinity = 1.0/0
#=> Infinity
irb> range = 10001..Numeric::Infinity
#=> 10001..Infinity
irb> range.include?( 234234234234134134134 )
#=> true
Edit: Actually, Ruby 1.9.2 seems to have added Float::INFINITY
, so if you're on the leading edge you can just use this.
Here is small tutorial about using Infinity in Rails
I'd suggest allowing the upper limit columns take NULL in the database. That will map to nil
in Rails, and is easily tested for, either with nil?
or with the fact that nil
is "falsey".
精彩评论