Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails validating virtual attributes

I this model:

class Bunny < ActiveRecord::Base
    attr_accessor :number
    validates_presence_of :number
    validates_numericality_of :number
end

Whenever I submit a form to create this model I get the following error:

undefined method `number_before_type_cast' for #<Bunny:0x103624338>

like image 689
tybro0103 Avatar asked Oct 12 '10 16:10

tybro0103


1 Answers

I fixed the problem by adding this method to my Bunny model:

def number_before_type_cast
    number
end

I don't like it, but I suppose it will work until someone posts a better solution.

like image 169
tybro0103 Avatar answered Oct 05 '22 19:10

tybro0103