RoR: Is there a value for infinite?

1.7k views Asked by At

I am trying to set up a max_users method. I have two different software types, hardware and software, and I am trying to set hardware to 1, and software to unlimited (eventually the user will choose in a form field how many max users they want, but for now just unlimited.

Any help is much appreciated. Thank you in advance.

Here's my model code:

def set_max_users
  if max_users.nil?
    self.max_users = 1
  end
end

And my JS that I'm using to dynamically add form fields with cocoon(in case this would help at all):

$ ->
  check_to_hide_add_link = ->
    max_users = parseInt($("#asset_max_users").val(), 10)
    if $("#assets_users .nested-fields").length >= max_users
      $("#assets_users .links a").hide()
    else
      $("#assets_users .links a").show()

  $("#assets_users").bind "cocoon:after-insert", ->
    check_to_hide_add_link()

  $("#assets_users").bind "cocoon:after-remove", ->
    check_to_hide_add_link()

  check_to_hide_add_link()
2

There are 2 answers

1
aspencer8111 On BEST ANSWER

Asked and answered here: How to express infinity in Ruby?

but the short of it is:

Float::INFINITY

0
Felix Borzik On

Although you can use Infinity in Ruby, you will not be able to save Infinity value to DB. As a workaround, you can set it to 0 (if you don't need max_user to be equal to 0, else you can just use nil/empty value) - as a result you'll just need to check if the value is 0, then it's Infinite and no need to check for more/less of other value. It will solve one more problem, users cannot set Infinity as a numeric field value.

check_to_hide_add_link = ->
  max_users = parseInt($("#asset_max_users").val(), 10)
  if max_users isnt 0 and $("#assets_users .nested-fields").length >= max_users
    $("#assets_users .links a").hide()
  else
    $("#assets_users .links a").show()