Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:30:08+00:00 2026-05-27T20:30:08+00:00

I have a price field for a product in a catalog. Sometimes the admin

  • 0

I have a price field for a product in a catalog. Sometimes the admin user is putting a comma when dealing with thousands (ex: $10,000) and sometimes he is just doing $6000. While I would like to simply tell him to do it one way or the other, I would also like to solve the issue programmatically.

The #show action responsible is here:

def show
       @category = Category.find_by_url_name(params[:category_id])
       @brand = Brand.find(params[:id])

       @search = Product.find(:all, :conditions => ['brand_id = ? and  category_id = ?', @brand.id, @category.id],
          :order=> params[:order] || 'price DESC')
       @products = @search.paginate(:page => params[:page], :per_page => 12 )

    @meta_title = "#{@brand.name}"
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @brand }
    end
  end

I also have a sort_options helper in my application helper that is providing the ordering options to the site user:

def product_sort_options
    options_for_select([
      ['', nil],
      ['Newest to Oldest', 'descend_by_date'],
      ['Oldest to Newest', 'ascend_by_date'],
      ['Price: Highest to Lowest', 'descend_by_price'],
      ['Price: Lowest to Highest', 'ascend_by_price'],
      ['Name', 'ascend_by_name']
    ])
  end

any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:30:09+00:00Added an answer on May 27, 2026 at 8:30 pm

    To make it a full answer – price should not be a string. The fact that you have 300 products now is not a big deal.

    Make a migration:

    rails generate migration decimalise
    

    Then edit it (db/migrate/*decimalise.rb), and write something like this:

    class Decimalise < ActiveRecord::Migration                                                                                                                                                                                       
      def up
        connection = ActiveRecord::Base.connection()
        # kill the weird chars in the string field
        connection.execute("UPDATE products SET price = REPLACE(REPLACE(price, ',', ''), '$', '')")
    
        # convert the string column into a decimal one
        change_table :products do |t|
          # adjust for your use case - this gives you values up to 9999999.99
          # if you need more, increase the 10
          t.column :price, :decimal, :precision => 10, :scale => 2
        end
      end
    
      def down
        change_table :products do |t|
          t.column :price, :string, :limit => 10
        end
      end
    end
    

    then finally, run

    rake db:migrate
    

    (untested, you will probably need to tweak. also, back up your DB before any tinkering – I’ll not be responsible for any data loss you suffer)

    EDIT One thing I forgot: how to print it out.

    <%= number_to_currency @product.price %>
    

    should give you something like $1,999.99 for a price of 1999.99.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have product search page in php/mysql. I have text field named price in
I have table 'products' and I need to update 'price' field by 20% if
I have a table called products containing a field called price and I simply
I have a Data class with one field, price. I referenced the price field
I have approximately 10,000 records. Each records has 2 fields: one field is a
I have online store that has simple products ( 1 product has 1 price
I have an object Orders (Order, Product, Cost, Price, Client, Day). I am retrieving
I have the following query: UPDATE PRODUCT SET FIXEDCOST = (Select PRICE from PRODUCTPROGRAM
I have two MySQL tables: product has id , title , price and some
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.