Forum: Ruby on Rails rails models, logging and testing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Surendra S. (Guest)
on 2006-03-01 17:13
(Received via mailing list)
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://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
|     -- Orwell, 1984, 1948
`----
Surendra S. (Guest)
on 2006-03-01 17:21
(Received via mailing list)
Surendra S. <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://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
|     -- Orwell, 1984, 1948
`----
Łukasz Piestrzeniewicz (Guest)
on 2006-03-01 17:33
(Received via mailing list)
On 01/03/06, Surendra S. <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).
Damon C. (Guest)
on 2006-03-01 19:31
(Received via mailing list)
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#Ra...

-damon
http://damonclinkscales.com/
Surendra S. (Guest)
on 2006-03-02 16:53
(Received via mailing list)
Hello,

Damon C. <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#Ra...
>
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://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "All animals are equal, but some animals are more equal than others."
|     -- Orwell, Animal Farm, 1945
`----
Pat M. (Guest)
on 2006-03-02 18:47
(Received via mailing list)
On 3/2/06, Surendra S. <removed_email_address@domain.invalid> wrote:
> >
> Surendra S.
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

You're probably just not including the fixtures for the associated
objects.
This topic is locked and can not be replied to.