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.
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!
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?
New fixtures in Rails 2.0 make it much easier to manage.
-------
locksmiths
The new fixture in Rails 2.0 is fantastic, thanks for sharing this post!
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.
Great writing and very useful info, when will an update be done?
Thanks! Wanted to leave a message after reading this.
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.
That looks very nice; cool demo and some really nice ideas as part of the new API. Congrats and good luck with the framework!
"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."
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!
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
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
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
Ut wisis enim ad minim veniam, quis nostrud exerci tution ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
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!
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
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
I know that when I first started using fixtures, I was wondering why these features weren't standard to begin with.
There are certainly a lot of details like that to take into consideration. That is a great point to bring up.






