Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Why is the "number_with_delimiter" method not recognized inside my model?

I have a simple validation:

class Product < ActiveRecord::Base
  # include ActionView::Helpers::NumberHelper
  ...
  validates_numericality_of :price, :less_than => 1000000, 
                            :message => "must be less than #{number_with_delimiter(1000000)}"                       
  ...
end

On this code, I have received the following error:

undefined method `number_with_delimiter' for #<Class:0x2665a58>

I tried to add:

include ActionView::Helpers::NumberHelper

but it didn't help.

What am I missing?

like image 551
Misha Moroshko Avatar asked Dec 17 '10 04:12

Misha Moroshko


2 Answers

The true problem here is that you're including this module into the class, rather than extended the class with it.

The differences is an include will make the methods available on the instance, where as the extend will make them where you're trying to use them: on the class.

For instance method use

include ActionView::Helpers::NumberHelper

For class method use

extend ActionView::Helpers::NumberHelper
like image 83
Ryan Bigg Avatar answered Sep 18 '22 19:09

Ryan Bigg


good answer, use :

ActiveSupport::NumberHelper::number_to_delimited(number, options={})

like image 29
Matrix Avatar answered Sep 18 '22 19:09

Matrix