#81
Nov 26

Fixtures in Rails 2.0

In Rails 2.0, fixtures see many improvements which makes them much easier to manage. Watch this episode for details.
Download (5.1 MB, 4:25)
alternative download for iPod & Apple TV (5 MB, 4:25)
Resources
# products.yml
couch:
  name: Couch
  price: 399.99
  manufacturer: lazyboy
  categories: furniture
tv_stand:
  name: TV Stand
  price: 149.95
  manufacturer: highdeph
  categories: furniture, electronics

# manufacturers.yml
lazyboy:
  name: LazyBoy
highdeph:
  name: HighDeph

# categories.yml
furniture:
  name: Furniture
electronics:
  name: Electronics

22 comments:

jbordnik Nov 26, 2007 at 01:25

Nice quick tip.


monkwitch Nov 26, 2007 at 05:15

very cool thanks


gampolt Nov 26, 2007 at 07:14

Thank you for sharing
It's cool trick


R. Elliott Mason Nov 26, 2007 at 07:58

Awesome. I know that when I first started using fixtures, I was wondering why these features weren't standard to begin with. I had no idea they would be in Rails 2.0. Very useful.


Peter Nov 26, 2007 at 08:40

Great site. All folks from our company can't wait for another piece of railscast ! Don't stop we love this site !


Jeroen Houben Nov 26, 2007 at 10:56

nice! What about has_many :through - is that managed the same way? And will this automatically work with rspec?


Ryan Bates Nov 27, 2007 at 09:36

@Jeroen, good questions! You'll need to create the join model fixtures for has_many :through, it won't work the same way as HABTM.

As for rSpec, fixtures work the same way there as they do with test/unit, so all of this is still valid.


Kevin Ruscoe Nov 27, 2007 at 14:32

These railscast's truly are awesome. Monday is now my favorite day.


Matthew Crouch Nov 29, 2007 at 05:42

Thank you very much, Rails Casts rule! Im new to rails, but your casts are helping my pick it up fast


Anthony Ettinger Nov 29, 2007 at 08:39

Nice example, like the simplicity...but when should I use a fixture? Surely maintaining a flat file for product/category management would be a step backwards for the shopkeeper?


Ryan Bates Nov 29, 2007 at 09:22

@Anthony, fixtures are primarily used in testing. They load up your database with data so the tests have something to work with.

I personally do not use fixtures extensively because I prefer to keep my tests less dependent on external data, but there are quite a few who do use them.


Paul M. Watson Dec 01, 2007 at 15:02

Thanks for the video Ryan, very useful.

And how do you test without "external" data?


The B Dec 03, 2007 at 21:31

@Paul: Do some investigation work on mocking and stubbing. It's wonderful.


mikem Dec 06, 2007 at 00:58

How do you deal with testing actions that require logged in user ? Is there a new way for doing this ?


Ryan Bates Dec 06, 2007 at 09:10

@mikem, I have a test helper method which does the login process. I just call that at the beginning of any test which needs to login.

This login method can do several things. One is to mock the @controller.current_user method to return an account of your choosing. Another is to load a fixture record if you're using that.


Joseph Dec 13, 2007 at 05:04

I upgraded to rails 2
using this command

gem install rails -y --source http://gems.rubyonrails.org

when I try to strip away all the 'id:' from my fixtures, test case failed. It complains that id cannot be found.

does anyone know what might possibly went wrong?


Adam Soltys Dec 15, 2007 at 23:12

I've created some 'foxy fixtures' but when I try to load them into my development database using "rake db:fixtures:load" I get SQL errors because the _id suffix doesn't seem to be added to the foreign key columns like I would expect it to be. Has anyone else noticed this when using rake db:fixtures:load?


Tellman Feb 12, 2008 at 18:50

Adam, I have the same problem. Scarce documentation and this cast claim the Foxy Fixtures approach works (with no explicit foreign key column names) - but it does not, at least by default.

I'm having trouble finding any useful information on the problem. is it possible that we need to include some module to the model classes?


huberto Feb 26, 2008 at 13:39

same problem adam and tellman, have a has_many/belongs_to relationship between restaurant and hour objects. it looks like this:

restaurants.yml

rest:
name: rest


hours.yml

lunch:
restaurant: rest

when i run it, i'm told there's no column in lunch called restaurant, so it doesn't know to add the id. oddly, if i change the line to restaurants: rest, it knows to hash the value to an id. of course, it still complains that there is no column because it's then looking for restaurants_id instead of restaurant_id.

anyone know what's wrong/i'm doing wrong?

cheers, h


Brian L Feb 27, 2008 at 10:32

One thing to note is that if your code is
dependent on the id of a model( I know, this is probably a bad implementation, but for right now, that is the way I have it), and so you put the id in the fixture so that your test will pass, for some reason, the fixture that belongs_to needs to have the point to the id of the fixture and not use the new shortcut of the name. i.e.
Model Timecard
belongs_to :hour_type
...
end

Model HourType
has_many: timecards
def vacation_hours
return self.hours if self.hour_type_id = 2
....
end
then in your fixtures
##RAILS_ROOT/rspec/fixtures/hour_types.yml
regular:
name: Regular
id: 1
vacation:
name: Vacation
id: 2

##RAILS_ROOT/rspec/fixtures/timecards.yml
weekla:
hours: 40
<b>hour_type_id: 1</b>
......
-------------------------------------
If you change the implementation you have to do this, but I thought I would mention the problem I had with this. It there is another solution to this, please let me know.


Daniel S Mar 12, 2008 at 02:57

Adam, Tellman

Do either of you, by any chance have any nested models? I have a few (e.g. Lookups::Country, Lookups::State, etc).

I've noticed that I only ever have problems on these models whereas other models that reference one another from within the models directory are done so with no problems.

I read somewhere about foxy fixtures not handling relationships between models where the :foreign_key parameter is used, but I'm not 100% sure.

For lack of experience and time, I haven't figured out a solution to the issue but I hope it sheds some light.


Marc-Antoine Duhaime Mar 19, 2008 at 11:37

And if I need to reset the database increment to 1 before running the fixture, how can I do it from Rails? I need that to be abble to run manuel test via the IRB like User.find(2).spouse.name, etc.

Add your comment:

(required)

(not displayed)

(SKIP THIS ONE)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player