Get Interval between two numbers in ruby on rails

I`m experiencing some difficulty to accomplish this task... I Have a Model called Contract, this model has an attribute called sequence that represents the contract sequence number, my question is: how can i get the interval between two typed numbers in a view with this single attribute?

Model

 class Contract < ActiveRecord::Base
    validates_presence_of :sequence
    validates_uniqueness_of :sequence
end

thanks in advance.

Ps: I was thinking about to create two distinguished inputs to receive the numbers... the questions is: how in the controller or model I create the loop for that!?

Upvotes: 0

Views: 790

Answers (1)

shivam
shivam

Reputation: 16506

You can use where:

Contract.where(:sequence => start_value..end_value)

Similarly in view you can do it like:

<div class="form-group"> 
  <%= f.label :sequence, :class => 'control-label col-lg-2' %>
  <div class="col-lg-10">
    <% Contract.where(:sequence => #{@start_value}..#{@end_value}).each { |seq|
      <%= f.text_field :sequence, :class => 'form-control', :value => seq %>
    <% } %>
  </div>
</div>

Upvotes: 1

Related Questions