#81 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:
- mp4Full Size H.264 Video (3.75 MB)
- m4vSmaller H.264 Video (3.47 MB)
- webmFull Size VP8 Video (11.9 MB)
- ogvFull Size Theora Video (6.46 MB)
Nice quick tip.
Thank you for sharing
It's cool trick
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.
Great site. All folks from our company can't wait for another piece of railscast ! Don't stop we love this site !
nice! What about has_many :through - is that managed the same way? And will this automatically work with rspec?
@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.
These railscast's truly are awesome. Monday is now my favorite day.
Thank you very much, Rails Casts rule! Im new to rails, but your casts are helping my pick it up fast
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?
@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.
Thanks for the video Ryan, very useful.
And how do you test without "external" data?
@Paul: Do some investigation work on mocking and stubbing. It's wonderful.
How do you deal with testing actions that require logged in user ? Is there a new way for doing this ?
@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.
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?
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?
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?
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
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.
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.
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.
But, is there a new way for testing it?
Daniel S:
Namespaced models do not seem to support the new 'named' fixtures. I just ran into this problem.
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!
This looks great. Wish I was using version 2.0.
When is the next version coming..
G (#30)
Just use "rake test" from the command line at the root of your rails app.
I was wondering how I would use fixtures to add category/sub category data? Is this even possible using fixtures?
Thanks for this help.
Nice quick tip.
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.
Hi Ryan, there is something wrong with this video. It stretched and cropped, so there is nothing to see.
Are you still using fixtures? It's interesting is there still need in using them or what are cases when you would need them? :) Thanks