#81
Nov 25, 2007

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

RSS Feed for Episode Comments 45 comments

1. jbordnik Nov 26, 2007 at 00:25

Nice quick tip.


2. monkwitch Nov 26, 2007 at 04:15

very cool thanks


3. gampolt Nov 26, 2007 at 06:14

Thank you for sharing
It's cool trick


4. R. Elliott Mason Nov 26, 2007 at 06: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.


5. Peter Nov 26, 2007 at 07:40

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


6. Jeroen Houben Nov 26, 2007 at 09:56

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


7. Ryan Bates Nov 27, 2007 at 08: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.


8. Kevin Ruscoe Nov 27, 2007 at 13:32

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


9. Matthew Crouch Nov 29, 2007 at 04:42

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


10. Anthony Ettinger Nov 29, 2007 at 07: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?


11. Ryan Bates Nov 29, 2007 at 08: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.


12. Paul M. Watson Dec 01, 2007 at 14:02

Thanks for the video Ryan, very useful.

And how do you test without "external" data?


13. The B Dec 03, 2007 at 20:31

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


14. mikem Dec 05, 2007 at 23:58

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


15. Ryan Bates Dec 06, 2007 at 08: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.


16. Joseph Dec 13, 2007 at 04: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?


17. Adam Soltys Dec 15, 2007 at 22: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?


18. Tellman Feb 12, 2008 at 17: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?


19. huberto Feb 26, 2008 at 12: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


20. Brian L Feb 27, 2008 at 09: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.


21. 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.


22. 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.


23. Jurgen Jessurun May 24, 2008 at 16:12

But, is there a new way for testing it?


24. Matthew Higgins Jun 11, 2008 at 15:52

Daniel S:
Namespaced models do not seem to support the new 'named' fixtures. I just ran into this problem.


25. Lucas Uyezu Jun 12, 2008 at 04:06

I was searching on the web for a way of expressing a one_to_many relation in fixtures, and found it only in your site.

Thanks for your work Ryan!


26. anon Sep 11, 2008 at 15:38

This looks great. Wish I was using version 2.0.


27. Schiphol Sep 12, 2008 at 07:55

When is the next version coming..


28. Kevin Jan 21, 2009 at 14:49

G (#30)
Just use "rake test" from the command line at the root of your rails app.


29. Ray Jan 22, 2009 at 22:25

I was wondering how I would use fixtures to add category/sub category data? Is this even possible using fixtures?


30. wobbe Apr 03, 2009 at 02:28

Thanks for this help.


31. 24 Hour Locksmith Dec 21, 2009 at 09:43

New fixtures in Rails 2.0 make it much easier to manage.

-------
locksmiths


32. Tjeerd Hagen Dec 21, 2009 at 10:49

Nice quick tip.


33. Emergency Locksmith Dec 28, 2009 at 10:29

The new fixture in Rails 2.0 is fantastic, thanks for sharing this post!


34. Crystal Admiral Jan 19, 2010 at 02:36

Great tip! Was looking for a way to expressing a one_to_many relation in fixtures, and I found it! The new fixture in Rails 2.0 is fantastic superior.


35. letty Mar 03, 2010 at 23:03

Thank you for sharing.


36. abercrombie & fitch Mar 03, 2010 at 23:06

abercrombie and fitch


37. Parkeren Schiphol Mar 31, 2010 at 09:03

Great writing and very useful info, when will an update be done?


38. Smart Parking Schiphol May 12, 2010 at 06:30

Thanks! Wanted to leave a message after reading this.


39. asics shose Jul 28, 2010 at 00:15

Great tip! Was looking for a way to expressing a one_to_many relation in fixtures, and I found it! The new fixture in Rails 2.0 is fantastic superior.


40. ipad converter Jul 29, 2010 at 00:30

That looks very nice; cool demo and some really nice ideas as part of the new API. Congrats and good luck with the framework!


41. online ged Jul 29, 2010 at 21:24

Amazing one, i appreciate this work....


43. linksys e2000 review Aug 07, 2010 at 03:11

"Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me."


44. michaeljordanshoes Aug 10, 2010 at 00:31

I think i can do it


45. vibram five fingers Aug 11, 2010 at 00:42

Hey, I read a lot of blogs on a daily basis and for the most part
people lack substance but
I just wanted to make a quick comment to say GREAT blog!…..
I’ll be checking in on a regularly now….
Keep up the good work!


46. merry Aug 12, 2010 at 01:11

nice post
look forward more
go for it
http://kicksbar.com


47. emrah eren Aug 13, 2010 at 15:56

hello thank you good information


48. jordans sneakers Aug 13, 2010 at 19:57

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat. Ut wisis enim ad minim veniam, quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. i jordans shoes</a> very much. Duis te feugifacilisi. Duis autem dolor in hendrerit in vulputate velit esse


49. authentic nike shoes Aug 14, 2010 at 01:20

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise,I was searching on the web for a way of expressing a one_to_many relation in fixtures


50. milgaussreplica Aug 16, 2010 at 01:30

thank you!


51. chanel handbags Aug 17, 2010 at 20:44

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gahttp://www.buyebag.com


52. chanel bags Aug 17, 2010 at 20:45

chanel bags


53. supra tk society Aug 18, 2010 at 18:52

good job,good article


54. nextags Aug 20, 2010 at 01:56

Ut wisis enim ad minim veniam, quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.


55. vibram five fingers Aug 23, 2010 at 05:29

Hey, I read a lot of blogs on a daily basis and for the most part
people lack substance but
I just wanted to make a quick comment to say GREAT blog!…..
I’ll be checking in on a regularly now….
Keep up the good work!


56. jordans 1 Aug 24, 2010 at 20:45

Nice blog, now, discount jordan shoesI found a new atmosphere here and you have a nice info to share, Air Jordan2 I really like this, and hope you’re always happy to share this every single day,thanks alot for your information, Air Jordan 1 I’ll bookmark this and share to all my friend for dropping at here, have a nice day… air jordan2 shoes,air force 1s


57. jordans 2 Aug 24, 2010 at 20:46

It is very well!


58. louis vuitton shoes Aug 26, 2010 at 02:21

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


59. jordan Aug 30, 2010 at 06:18

jordan My readers have about the same interets


60. snow boots Aug 31, 2010 at 00:45

I know that when I first started using fixtures, I was wondering why these features weren't standard to begin with.


61. louis vuitton sunglasses Sep 01, 2010 at 22:42

There are certainly a lot of details like that to take into consideration. That is a great point to bring up.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source