by g on Aug 08, 2017
Last month I noticed the lack of a good ActiveRecord tutorial/screencast that goes from basic to advanced usage (with examples). So, I decided to put together a presentation on ActiveRecord for the Orlando Ruby Users Group.
UPDATE If you already know the basics (belongs to, has many), I then you might want to check out Advanced ActiveRecord, an Envycast us put together.
Otherwise the video below should get you started. You can download all the example code
The video covers these topics:
- Who invented ActiveRecord and why
- How to use ActiveRecord
- Basic model to table mapping
- has_many relationships
- Magical counter fields
- Many to many relationships
- has_many => through
- Polymorphic relationships
If you have a few minutes over lunch please take a look, and let me know if you have any questions/comments.
Sorry, comments are closed for this Post, but feel free to email us with your input. We'd love to hear it.
Good little tutorial on the concept behind Active Record, I just have one question about the Rails implementation though. Why would it need a counter column instead of doing a join with a count. I would think that the counter column would lead to a possible race condition where it’s trying to be updated by two different objects at the same time.
Great stuff. A good introduction to ActiveRecord.
To Matt Filizzi:
A counter column is much faster to query than a join. You can’t update the same row at the exact same time so that won’t be a problem.
Great tutorial. It even brought back the counter_cache trick for me. Must admit I’ve done exactly that in custom code. Bring us more! More!
But why didn’t you use hasandbelongs_to_many to illustrate the many-to-many relationship? Seems like a good ole basic thing to cover.
joost, in a world where we do table to object mapping, I’m just not a big fan of having an extra table which doesn’t have a model associated with it. You can have the same exact behavior with has_many through.
Plus I always end up adding additional fields in my join tables, so having it as a model is usually convenient.
Also, sometimes I just want to create the join relationship without retrieving all the objects. If I know the userid and the articleid, I might not want to fetch those objects, when I could just do:
UserArticle.create(:userid => 3, :articleid => 4)
I guess it’s just personal preference though. I figured for the tutorial it’d be better to do it this way because it’s less limiting in the long run.
I agree, and even think think habtm should be depreciated at some point! creating a model for your join is a great practice..
Finding a decent name for your join/through often makes you think WHY you have the join, and encourages you to move logic onto the join-model that you may not have considered could live there previously
The problem with having a model for basic habtm relationships, of course, is that every association in that relationship instantiates an extra model object, which is the join model. I think that’s overhead you can live without if you don’t need it. Plus, it’s not terribly hard to use a regular habtm relationship and, when you see you need things in the join table, create a join model and switch to has_many :through.
@shadowfiend: I’ve tried that, but how do you convert a primary-keyless table to one with a primary key? ActiveRecord habtm doesn’t work well if the join table has an id.
When you switch to hm :t, you’d have to run through all the records in the table and assign primary keys to all the rows, which seems more awkward than just starting out with a full join model to start with.
Great thanks for your job!
I’m core member of China BeiJing RoR Group, and I’ll use this tutorial as a reprentation on ActiveRecord for the group training tomorrow.
Thank you once more.
Very nice little flick. I especially liked the communication problem between sql cat and application boy..lol
You just earned some serious awesome points in my book
Super! I’ve just started RoR and this video will go great with DHH’s Agile book I’m reading. Thanks for helping out us new guys!!
@topfunky - well, you might have to drop into SQL (I’m not certain whether this is within the migration syntax), but if you just create a new auto-incrementing column, I’m pretty sure MySQL will generate the values for the existing rows. Or am I not getting what you were asking?
sorry for beating an old horse here, but if things (the id bug) were patched then
@topfunky, @shadowfiend: migrating from habtm to hmt would’ve been seamless, like it always should’ve.
You might want to read about deinterlacing when encoding vidoes, because at the moment it hurts my eyes to watching this .)
Great ActiveRecord tutorial, thanks!
My eyes only started to hurt after I watched SQL cat and Ruby boy for the 5th time. We loved it. My wife and I watched this over dinner and I have to say you did a great job with a basic to advanced A R tutorial; which was the original goal - mission accomplished!
Actually it was the scotch bonnet I got in my eyes that made my eyes hurt. Keep up the great work, and you are awesome producers.
Thanks for this. It’s always good to get back to basics and get a full explanation of the details. Often with Rails I “just figure it out” and forget to learn the why. That concept is actually one of the biggest takeaways from your screencast.