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?
To make it a full answer –
priceshould not be a string. The fact that you have 300 products now is not a big deal.Make a migration:
Then edit it (
db/migrate/*decimalise.rb), and write something like this:then finally, run
(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.
should give you something like
$1,999.99for a price of1999.99.