# Rails models, logging and testing

**URL:** <https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249>\
**Category:** Rails\
**Created:** [March 1, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249 "2006-03-01T16:13:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/1 "2006-03-01T16:13:45Z")

</div>

Hello,  
I am using rails 1.0 and I have following questions. Please help me.

[1] How can I do logging in the rails models? I checked the wiki but  
didn’t find  
anything which I explained how to do logging in models.

[2] Say I have a certain model, when I declare a function in the model,  
and  
try to use that function from a view or a unit test, I get an error. For  
views  
the error occurs only once during the first execution but for unit tests  
it is  
repeatable.

model file:  
class MyModel \< ActiveRecord::Base  
def abc  
“ok”  
end  
end

view file:  
\<%= @mymodel.abc %\>

What am I doing wrong? Is it something strange and unexpected?

[3] During testing, if I have a fixture file called products.yml and  
some  
named entries in it, I get an error if I try to use those named entries  
as  
variable named in my unit tests.

The agile book says:

“Each named fixture is also automatically ?found? using  
an Active Record finder method and put in an instance variable named for  
the fixture.”

But when I am trying to repeat this I am getting errors, am I missing  
something or doing something wrong or have other people also experience  
the  
same problem?

Thanks.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”  
| – Orwell, 1984, 1948  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/2 "2006-03-01T16:21:22Z")

</div>

Surendra S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hello,  
> I am using rails 1.0 and I have following questions. Please help me.
> 
> [1] How can I do logging in the rails models? I checked the wiki but didn’t find  
> anything which I explained how to do logging in models.

## I take back this question. Sorry, for creating unnecessary noise.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”  
| – Orwell, 1984, 1948  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/3 "2006-03-01T16:33:41Z")

</div>

On 01/03/06, Surendra S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I am using rails 1.0 and I have following questions. Please help me.
> 
> [1] How can I do logging in the rails models? I checked the wiki but didn’t find  
> anything which I explained how to do logging in models.

Use following statement:

RAILS\_DEFAULT\_LOGGER.info “This will be logged with ‘info’ level”

But please not use it in your models. Rather, use it in your tests.  
Then even if you forgot to remove logging statements they will not  
have impact on performance or log size of your application.

> [2] Say I have a certain model, when I declare a function in the model, and  
> try to use that function from a view or a unit test, I get an error. For views  
> the error occurs only once during the first execution but for unit tests it is  
> repeatable.  
> What am I doing wrong? Is it something strange and unexpected?

Doesn’t look like. Could you provide more info on error?

> But when I am trying to repeat this I am getting errors, am I missing  
> something or doing something wrong or have other people also experience the  
> same problem?

From Rails 0.14 and onwards fixtures are no longer put in instance  
variables. You should either use:

fixtures :users  
users(:bob)

or modify the file test/test\_helper.rb and change value of  
self.use\_instantiated\_fixtures to true. Second method will slow down  
tests (in case of large fixture sets considerably).

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/4 "2006-03-01T18:31:15Z")

</div>

Surendra S. \<efuzzyone@…\> writes:

> [1] How can I do logging in the rails models? I checked the wiki but didn’t  
> find anything which I explained how to do logging in models.  
> logger.info “blah”

> [3] During testing, if I have a fixture file called products.yml and some  
> named entries in it, I get an error if I try to use those named entries as  
> variable named in my unit tests.

Read this for more info on testing:

[http://www.clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting](http://www.clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting)

-damon

> **[Damon Clinkscales](https://damonc.onuniverse.com/)**
>
> Damon Clinkscales

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 2, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/5 "2006-03-02T15:53:49Z")

</div>

Hello,

Damon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Surendra S. \<efuzzyone@…\> writes:
> 
> > [3] During testing, if I have a fixture file called products.yml and some  
> > named entries in it, I get an error if I try to use those named entries as  
> > variable named in my unit tests.
> 
> Read this for more info on testing:
> 
> [http://www.clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting](http://www.clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting)

Damon and Åukasz thanks for the help.

While writing unit tests for a model, I noticed that I am not able to  
use the  
methods generated by different associations. How do I use and test them?

Thanks once again.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 2, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/rails-models-logging-and-testing/75249/6 "2006-03-02T17:47:35Z")

</div>

On 3/2/06, Surendra S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Surendra S.  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

You’re probably just not including the fixtures for the associated  
objects.
