Gibson
Gibson

Reputation: 2085

Which attribute type should I use for price?

I want users to select a price range (collection), but they will also have the option "I don't know", so I can't make Price an integer because the "I don't know option" will not save to the ddbb... Should I make Price a string?

NOTE: Afterwards I will make an advanced search with "Search by price"

Update #1

I want the user to select a price from a price range. In that price range Ill have also 2 more options: "I don't know", and "Fixed price". If the user selects I don't know, how can I handle this? If the user selects "Fixed price", how can I tell Simple form I need a new input for this new value?

Update #2

My collection:

<%= f.input :price, collection: ["0-10","11-20","21-50","51-100","101-200","201-500","500-1000","1000-2000","2000-4000", "Más de 4000"], label: "¿Cuanto te costó?" ,placeholder: "Euros aproximados", html_input: "required", input_html: {class: 'ipt'} %>

Update #3

I added this:

<%= f.input :price, collection: ["-2","-1","0-10","11-20","21-50","51-100","101-200","201-500","500-1000","1000-‌​2000","2000-4000", "Más de 4000"], label: "¿Cuanto te costó?" ,placeholder: "Euros aproximados", html_input: "required", input_html: {class: 'ipt'}, id: "price" %> 

With price as an id, but it won't work. When I select -2 option, It wont show another input. NOTE: I'm inside a Modal, can It be the reason for it?

Update #4

Everything works fine, but I have a problem. Values are stored as -1, -2 only, so when I want to print a products price range , It prints -2. How can I deal to print ranges and "I don't know" values when they are being stored as -1 and -2 in the ddbb? Should I start to think about saving things as strings instead of integers? How do You think I should deal with this?

Another solution? Thanks!

Upvotes: 0

Views: 314

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

Since price range cannot be a negative integer, you can have "I don't know"'s value to be -1 and save it, so that:

  • It saves memory (Integer is better than String).
  • You can have the same data type.
  • Money with -1 value is "I don't know".
  • Money with -2 value is "Fixed Price".

Update

Now this is getting complex. Use JavaScript to do this. After putting your collections this way:

collection: ["-2","-1","0-10","11-20","21-50","51-100","101-200","201-500","500-1000","1000-2000","2000-4000", "Más de 4000"]

Use jQuery to manipulate the input. Say, if it generates the code like this:

<select id="price" name="price">
    <option value="-2">-2</option>
    <option value="-1">-1</option>
    <option value="0-10">0-10</option>
    ...
    ...
</select>

Now use jQuery and do something like this:

$(document).ready(function(){
    $("#price").find('option[value="-2"]').html("Fixed Price!");
    $("#price").find('option[value="-1"]').html("I don't know!");
    $("#price").change(function(){
        if ($(this).val() == -2)
            $('<input type="text" name="customPrice" />').insertAfter(this);
    });
});

Upvotes: 1

Related Questions