# Problems with validation on STI

**URL:** <https://www.ruby-forum.com/t/problems-with-validation-on-sti/143311>\
**Category:** Rails\
**Created:** [July 17, 2008, 8:18am UTC](https://www.ruby-forum.com/t/problems-with-validation-on-sti/143311 "2008-07-17T08:18:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 17, 2008, 8:18am UTC](https://www.ruby-forum.com/t/problems-with-validation-on-sti/143311/1 "2008-07-17T08:18:30Z")

</div>

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

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 !@d1.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:in` **send** ’  
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?

---

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 17, 2008, 8:45am UTC](https://www.ruby-forum.com/t/problems-with-validation-on-sti/143311/2 "2008-07-17T08:45:40Z")

</div>

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_

---

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 17, 2008, 10:34am UTC](https://www.ruby-forum.com/t/problems-with-validation-on-sti/143311/3 "2008-07-17T10:34:41Z")

</div>

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
