Problems with validation on STI

I have the following STI table:

def self.up
create_table :distributions do |t|
t.string :type
t.integer :simulation_id
t.string :dist_name
t.string :desc, :default=> ‘fixed’
t.float :param1, :default => 5.0
t.float :param2, :param3
t.timestamps

  #fields for RscDist
  t.integer :resource_id

  #fields for LoadDist
  t.integer :load_id
end

end

My model file looks like this:

class Distribution < ActiveRecord::Base

validates_presence_of :desc, :param1
validates_uniqueness_of :dist_name, :scope => "simulation_id"
 validates_inclusion_of :desc,
                    :in => %w{fixed exponential normal uniform

triangular},
:message => “should be ‘fixed’, ‘exponential’,
‘normal’, ‘uniform’, ‘triangular’”
validates_numericality_of :param1, :param2, :param3

def to_s
    "desc = " + desc + "  param1 = " + param1.to_s + " dist_name = "
  • dist_name + " type = " + type.to_s
    end

    protected
    def validate
    #desc can be fixed, exponential, normal, uniform, triangular
    errors.add(“need 2nd parameter”) if desc == ‘normal’ || desc ==
    ‘uniform’ || desc == ‘triangular’
    errors.add(“need 3rd parameter”) if desc == ‘triangular’
    end

end

class LoadDist < Distribution

validates_uniqueness_of :dist_name, :scope => "load_id"

belongs_to :load

end

class RscDist < Distribution
validates_uniqueness_of :dist_name, :scope => “resource_id”

belongs_to :resource

end

In my unit tests I can’t seem to get a valid object. Here’s my setup:

def setup
@s1 = simulations(:base)
@rsc1 = resources(:rsc1)
@d1 = RscDist.new
@d1.simulation_id = @s1.id
@d1.resource_id = @rsc1.id
end

and then the test:

def test_params_exp

@d1.desc = 'exponential'

assert [email protected]valid?

@d1.param1 = 14
@d1.dist_name = 'dist1'
puts @d1.to_s
assert @d1.valid?

end

The final line “assert @d1.valid?” fails, even though I have a :desc
field that is properly spelled, a numerical :param1, and a unique
:dist_name. The message is:

  1. Failure:
    test_params_exp(DistributionTest)
    [test/unit/distribution_test.rb:35:in test_params_exp' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testi ng/setup_and_teardown.rb:33:insend
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testi
    ng/setup_and_teardown.rb:33:in `run’]:
    is not true.

What am I doing wrong?

I think I found the problem. I was expecting empty attributes for desc
and param1, but I set those to ‘fixed’ and 5 by default. embarrassed
grin

On 17 Jul 2008, at 07:18, Louise R. wrote:

The final line “assert @d1.valid?” fails, even though I have a :desc
field that is properly spelled, a numerical :param1, and a unique
:dist_name. The message is:

I don’t know what the problem is off the top of my head, but after
calling @d1.valid?, calling
@d1.errors will show you the validation failures which should help you
in tracking down the problem

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs