0

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!?

1 Answer 1

1

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>
4
  • Hi, ok, thanks, but in view? how do I do that? I mean, how can I do two inputs for one attribute? <div class="form-group"> <%= f.label :sequence, :class => 'control-label col-lg-2' %> <div class="col-lg-10"> <%= f.text_field :sequence, :class => 'form-control' %> </div> </div> Commented May 19, 2015 at 14:37
  • Something wrong in your code? (Just to remind) My Model doesn't have start_value and end_value, I'm maybe explaining or understanding it wrong (but anyway, thanks for the help) but I need a way to include two inputs and do the loop from it. Commented May 19, 2015 at 14:50
  • Its a sudo code. Ive not tried it actually. But it should work fine, you just have to tweak it as per your codebase
    – shivam
    Commented May 19, 2015 at 14:51
  • Do you have (and can I add you?) gtalk!? Commented May 19, 2015 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.