Forum: Ruby on Rails where to put helper fns for fixtures?

Posted by Fearless Fool (fearless_fool)
on 2010-03-07 20:55
I'm writing a fixture in test/fixtures/parrots_model.yml, and I'm
happily using ERB to flesh out my fixture.  Suddenly, I realize that I
need a helper function to call from my erb code, such as:

polly:
  subtype: parakeet
  weight_kg: <%= ounces_to_kg(22.3) %>

alex:
  subtype: african gray
  weight_kg: <%= ounces_to_kg(170.3) %>

Where's the right place to stash the "ounces_to_kg()" function so that
it's available when I rake db:load:fixtures?  (And does the answer
change if its a method specific to the parrot model?)

tia.
Posted by Marnen Laibow-Koser (marnen)
on 2010-03-09 18:48
Fearless Fool wrote:
> I'm writing a fixture in test/fixtures/parrots_model.yml, and I'm
> happily using ERB to flesh out my fixture.  Suddenly, I realize that I
> need a helper function to call from my erb code, such as:
> 
> polly:
>   subtype: parakeet
>   weight_kg: <%= ounces_to_kg(22.3) %>
> 
> alex:
>   subtype: african gray
>   weight_kg: <%= ounces_to_kg(170.3) %>
> 
> Where's the right place to stash the "ounces_to_kg()" function so that
> it's available when I rake db:load:fixtures?  

Nowhere, because IMHO it's never right to use Rails fixtures.  Use 
factories instead -- I'm fond of Machinist, but there are other 
solutions out there.  This will solve your problem and also give you 
many other advantages.

Fixtures are a horribly broken feature of Rails.  Don't bother with 
them.

> (And does the answer
> change if its a method specific to the parrot model?)
> 
> tia.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Fearless Fool (fearless_fool)
on 2010-03-10 02:14
Marnen Laibow-Koser wrote:
>> ...snip...
>> Where's the right place to stash the "ounces_to_kg()" function so that
>> it's available when I rake db:load:fixtures?  
> 
> Nowhere, because IMHO it's never right to use Rails fixtures.  Use 
> factories instead -- I'm fond of Machinist, but there are other 
> solutions out there.  This will solve your problem and also give you 
> many other advantages.
> 
> Fixtures are a horribly broken feature of Rails.  Don't bother with 
> them.

Thanks Marnen -- very helpful.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.