# Adding new variables to a model

**URL:** <https://www.ruby-forum.com/t/adding-new-variables-to-a-model/193572>\
**Category:** Rails\
**Created:** [August 19, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/adding-new-variables-to-a-model/193572 "2010-08-19T21:03:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madison\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@Madison\_K](https://www.ruby-forum.com/u/Madison_K)\
**Post date:** [August 19, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/adding-new-variables-to-a-model/193572/1 "2010-08-19T21:03:42Z")

</div>

Hello –

Thanks in advance for putting up with a newbie question:)

I added two columns to my Products model. The db migration completed  
without  
error, I added the items to the views, and I tested the expected  
behavior on  
the site itself. The properties can be set, stored, and is displayed  
later.  
I also put some validation in the model, and the errors display  
perfectly.  
So everything is working just as I would expect.

Where I ran into trouble is when I ran the unit tests I have for the  
products model. I’m getting errors that say the properties are an  
“undefined  
method”. Even for test cases that don’t refer to these properties, they  
are  
returning the same error. I assume this is from referencing the model.  
Why  
does everything work correctly on the site, but the test cases don’t  
seem to  
recognize the new properties I’ve added to the model?

Thanks!  
Madison

Additional information below:

(note: two new columns are has\_case and case\_quantity)

_Error:_  
C:\webapp\depot\>ruby -I test test/unit/product\_test.rb  
Loaded suite test/unit/product\_test  
Started  
EEEEEE  
Finished in 0.218401 seconds.

1. Error:  
test\_default\_case\_values(ProductTest):  
NoMethodError: undefined method `has_case' for #<Product:0x8c65b60> test/unit/product_test.rb:53:in`test\_default\_case\_values’
2. Error:  
test\_if\_has\_case\_must\_have\_case\_quantity(ProductTest):  
ActiveRecord::UnknownAttributeError: unknown attribute: has\_case  
test/unit/product\_test.rb:58:in `new' test/unit/product_test.rb:58:in`test\_if\_has\_case\_must\_have\_case\_quantity’
3. Error:  
test\_image\_url(ProductTest):  
NoMethodError: undefined method `case_quantity' for #<Product:0x8c606d0> test/unit/product_test.rb:36:in`test\_image\_url’  
test/unit/product\_test.rb:34:in `each' test/unit/product_test.rb:34:in`test\_image\_url’
4. Error:  
test\_invalid\_with\_empty\_attributes(ProductTest):  
NoMethodError: undefined method `case_quantity' for #<Product:0x8c5db20> test/unit/product_test.rb:10:in`test\_invalid\_with\_empty\_attributes’
5. Error:  
test\_positive\_price(ProductTest):  
NoMethodError: undefined method `case_quantity' for #<Product:0x8c5af10> test/unit/product_test.rb:20:in`test\_positive\_price’
6. Error:  
test\_unique\_title(ProductTest):  
NoMethodError: undefined method `case_quantity' for #<Product:0x8c57988> test/unit/product_test.rb:47:in`test\_unique\_title’  
6 tests, 0 assertions, 0 failures, 6 errors  
C:\webapp\depot\>

_Model:_  
class Product \< ActiveRecord::Base  
has\_many :orders, :through=\> :line\_items  
has\_many :line\_items  
validates\_presence\_of :title, :description, :image\_url, :price  
validates\_numericality\_of :price, :case\_quantity  
validate :price\_must\_be\_at\_least\_a\_cent, :if\_case\_must\_have\_quantity,  
:if\_quantity\_must\_have\_case  
validates\_uniqueness\_of :title  
validates\_format\_of :image\_url,  
:with =\> %r{.(gif|jpg|png)$}i,  
:message =\> 'must be a URL for GIF, JPG ’ + ‘or  
PNG  
image.’  
protected  
def price\_must\_be\_at\_least\_a\_cent  
errors.add(:price, ‘should be at least 0.01’) if price.nil? || price  
\<  
0.01  
end  
def if\_case\_must\_have\_quantity  
if (has\_case == true) && (case\_quantity \< 1)  
errors.add(:case\_quantity, ‘: must set case quantity if this  
product  
can be sold in cases’)  
end  
end  
def if\_quantity\_must\_have\_case  
if (has\_case == false) && (case\_quantity \> 0)  
errors.add(:case\_quantity, ‘: must be 0 if product is not sold in  
cases’)  
end  
end  
def self.find\_products\_for\_sale  
find(:all, :order =\> “title”)  
end  
end

_DB migration:_  
class AddCaseAndQuantityToProducts \< ActiveRecord::Migration  
def self.up  
add\_column :products, :has\_case, :boolean, :default =\> false  
add\_column :products, :case\_quantity, :integer, :default =\> 0  
end  
def self.down  
remove\_column :products, :has\_case  
remove\_column :products, :case\_quantity  
end  
end

---

<div class="post-metadata">

**Author:** ![Madison\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@Madison\_K](https://www.ruby-forum.com/u/Madison_K)\
**Post date:** [August 19, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/adding-new-variables-to-a-model/193572/2 "2010-08-19T21:17:45Z")

</div>

On Aug 19, 2010, at 12:02 PM, Madison K. wrote:

> Hello –
> 
> Thanks in advance for putting up with a newbie question:)
> 
> I added two columns to my Products model. The db migration completed without error, I added the items to the views, and I tested the expected behavior on the site itself. The properties can be set, stored, and is displayed later. I also put some validation in the model, and the errors display perfectly. So everything is working just as I would expect.
> 
> Where I ran into trouble is when I ran the unit tests I have for the products model. I’m getting errors that say the properties are an “undefined method”. Even for test cases that don’t refer to these properties, they are returning the same error. I assume this is from referencing the model. Why does everything work correctly on the site, but the test cases don’t seem to recognize the new properties I’ve added to the model?

Don’t you need to setup the test database?

rake db:test:clone # Recreate the test database from the  
current environment’s database schema  
rake db:test:clone\_structure # Recreate the test databases from the  
development structure  
rake db:test:load # Recreate the test database from the  
current schema.rb  
rake db:test:prepare # Check for pending migrations and load  
the test schema  
rake db:test:purge # Empty the test database

---

<div class="post-metadata">

**Author:** ![Madison\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7c8e57/32.png) [@Madison\_K](https://www.ruby-forum.com/u/Madison_K)\
**Post date:** [August 19, 2010, 9:24pm UTC](https://www.ruby-forum.com/t/adding-new-variables-to-a-model/193572/3 "2010-08-19T21:24:47Z")

</div>

Oh of course!! I can’t believe I forgot that.

Thanks so much!!!
