Skip to content
486: ActiveModel Everywhere episode artwork

Dec 23, 2025 27 min

486: ActiveModel Everywhere

from The Bike Shed Hosted by thoughtbot

Aji and Sally join forces to discuss the different ways they utilise active models in their workflows. Aji describes a new system for working with active models they’ve been using recently, Sally recalls a project where active models could have saved her a lot of time, before putting their heads together to think of new creative ways to utilise rails’ tools toolset to build other active models. — Discover more of The Magic of Rails through Eileen Uchitelle’s Keynote, or check out the GitHub repo mentioned in this episode. Thanks to our sponsors for this episode Judoscale - Autoscale the Right Way (check the link for your free gift!), and Scout Monitoring. Your hosts for this episode have been thoughtbot’s own Sally Hall and Aji Slater. If you would like to support the show, head over to our GitHub page, or check out our website. Got a question or comment about the show? Why not write to our hosts: [email protected] This has been a thoughtbot podcast. Stay up to date by following us on social media - YouTube - LinkedIn - Mastodon - BlueSky © 2025 thoughtbot, inc.

Episode details come from the podcast's RSS feed.

Embed this episode

Ready to play

486: ActiveModel Everywhere

0:00 27:11
of matches

Transcript Auto-generated, may contain errors

Autoscaling is a simple concept. Automatically add resources when needed, and automatically shut them down to avoid paying for excess capacity. How hard could that be? If you've set up Autoscaling yourself, you know it's not that easy, especially using the native Autoscalers on platforms like AWS and Heroku. That's why you need JudoScale.

JudoScale is Autoscaling as a service, and they make Autoscaling simple and easy, as it should be. You can use JudoScale on AWS, Heroku, Render, Fly.io, and more. It's free for low traffic apps, and unlimited plans start at $25 per month. Autoscale on easy mode at judoscale.com. Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtPot about developing great software.

I'm Adi Slater. And I'm Sally Hall, and together we're here to share a bit of what we've learned along the way. So Sally, what's new in the world? Very non-deculated, but this is my handmade holiday sweater debut. Amazing.

Wearing for the first time, which I'm very excited about. It only took me 14 months to make. Wait, so you made that yourself? I made this. I'm very proud of it.

I made it using a pattern from this company called Pacific Coast Nits, I think, and they do what they call doodles, which is very fun. You get a base pattern, and then they make all these different sets of doodle cards, which are like little colorwork patterns, and you can combine them in different ways to make a sweater or scarf or a hat or whatever. So not only did I make this, I designed it, designed it in quotes, because I took the little cards that they had and planned out exactly which motif would go where. Wow. Which is, I think, a very fun way to knit.

Yeah, that seems like a fun way to do a lot of different creative projects. Now, for the listeners who aren't able to see the sweater, I think I've lost a little bit of detail, because as you get closer, I can see more about it, but it's kind of a blue base. Yeah, and it's a fair-aisle sort of type thing where the yoke is different patterns, the most visible which are holly leaves and snowflakes and candy cane stripes. I wonder if that's named after a fair-aisle in Scotland. It is.

Yeah, there's so much history to that, but we don't know that. It's not what this is. Yeah, absolutely. Well, it looks amazing. Thank you.

I'm really enjoying it, and it is a quote, snow day here in North Carolina, which means it might snow so everything is closed. It feels like an appropriate idea to do that. Yeah, absolutely. What's new in your world, Audrey? Actually, what's new in my world is I've been thinking about a couple of things from the last episode that I recorded with Joel.

Well, I haven't heard that yet, so I'd love to hear more about that. I think actually, so it hasn't gone out at the time that we are recording, but I'm sure we have gotten a lot of letters because that's something in the episode that is not true. We were talking about how cosmic rays can flip bits in computers. Okay. And this is a thing that does happen, and I mentioned that there was a speedrunner streaming on Twitch or something playing a Mario game, and something incredible happened, and the main running theory was that a cosmic ray had flipped one of the bits in the ongoing process of the game.

And it turns out that's probably not true, but re kind of digging into the whole world of that after I set a thing, and then immediately after the episode found out that it was wrong, still fascinated by the fact that that does happen. That is a thing that just happens. You can't really protect against it as a normal computer user, and every now and then the universe will just flip one of your bits. The universe will just be like, you thought you knew everything and had control, but no. Exactly.

I find that strangely comforting. I don't know. Maybe it's more than I like having something to blame. At work time I'm ashamed of everything because I don't know. Yeah, the fact that you can do everything right and still sometimes fail can be either incredibly depressing or very grounding.

Yeah, a little bit of that thing. Yeah, that's true. Yeah. The other thing I've been thinking about is that we didn't mention that this is the second season of the bike chest. So while this is not the first episode of the second season of the bike shed, welcome everybody to the second season of this new format of the bike shed.

We can pretend like we did it on purpose. It's the second episode of the second season. Oh, that's good. Now I need to write that down to make sure we do it on the third episode. We will definitely get that.

That's okay. 100%. Great. Two non-program and current working related new in our worlds for the day and we'll go right along into the topic that we have decided to talk about here. And that is I have titled active model everywhere.

Is there anything that that makes you think of when you just hear that phrase? I feel like it makes active models sound more exciting than it usually does. My first reaction is, ooh, interesting. Tell me more because I feel like whenever I'm working in a class and maybe that does not include active model, I kind of wish I was. Mm-hmm.

I think that's exactly what this is referring to. And it's a little, I don't know, mantra that I've been following maybe whenever I'm starting up a new service object. Let's just say, although I think that term is a little unnecessary in general. If there's something in a rail system that doesn't fit into a database table-backed model, the way that we usually think about model and active record model, we'll call it like a service object or something like that or just use a PORO, which is a plain old Ruby object. But I would advocate to think about reaching to include some of active model every time that you do that.

And a couple of reasons why I like that. You can create a consistent API across all of the little objects that are in your system that are talking together. That way, a controller that is acting on a non-database table-backed object will still look by and large. The same sort of shape as all your other controllers. And you can have this kind of consistent clarity of thought at a high level across the different paths through your application.

And also, it gives you so much for free and so much like robust, well-tested things that you already trust to use in your system for your active record models. But you can, without having to rewrite something like validators or attributes or different things like this, the ways they get instantiated, you've got a kind of robust system backing up each of these things that are common things to have to do in these types of classes. Have you ever tried to debug an issue in production while switching between your error tracker, log viewer, and APM tool? Maybe you know how painful it is when you can see the error but can't find the relevant logs or when you see slow performance but it can't connect to the root cause.

Scout monitoring is an all-in-one platform built to make your life easier. With log management, performance tracking, and error monitoring coming soon. All of your data will be together at last. We'll go from bug to fix in one place. The result?

Less context switching, faster debugging, and monitoring that actually helps you ship code instead of distracting from it. No DevOps team? No problem. Scout works with your existing stack and gives you insights in five minutes, not hours. From bug to fix in one place at ScoutAPM.com.

I'm now remembering a time a few years ago when I did do this and it ended up being really helpful. I guess before I jump into the time I did, I would like to hear about where you're doing it these days to see if I'm using active model the same way before I go down that tangent that is entirely not what you were wanting to talk about. I'm going to do that anyway, but you can have my answers. So I think lately there was a conversation in Slack that kind of brought this top of mind in the Thoughtbot Slack. Someone was asking about making a new controller and it didn't map exactly to one of the active record models and it has some kind of cross-coming concerns and we were kind of going back and forth until someone came in and just hit the nail on the head that tried to think of everything in your Rails app as a resource.

As soon as you're thinking about it as a thing, as a resource in your domain, then all of those behaviors that you get from active model make a whole lot of sense and become really useful. I've been using that just today to make a class that is, let me see what I called it, it's about adding some default associations to a record. The action that we needed to take is that this one record can have a bunch of different associations. It needs to have some associations. They might be custom, they might be special, but by and large when they first enter the system, they're going to get a default set.

While we're using administrator, one of the ways that we want to make this easier for someone setting up a new record in the system is we'll give them a button to just add all the default themes. We're attaching themes to this model. You can also use it in a has many multi-select dropdown, like usual, if you have something specific that this is going to require. The resource that I thought of was default themes. You are creating and saving a set of default themes.

Thinking about the RESTful actions and the active record API methods that you use to persist things because we're going to persist something. I made a .save on my default themes class so that the controller looks exactly like a Rails controller and everyone will be familiar with what the flow is and everything is right off the bat looking at it. But this .save is not writing to a database table. It is, but not in the way that active record models usually would because it is calling out to an active record model saving there, making an association through a join table. There is persistence to a database, but it's not the .save that you get from an active record object.

It's not like a one to one. Now we've got a table called database theme and we're writing a new record to it that is just its own thing. Exactly. Exactly. So you'll pass in that original resource and then it will query for all of the default type associations, join them together and either give you back errors or a success.

Yeah, that does sound useful. It's sort of like the idea of other pieces of the app that will be interacting with this not really needing to care or know about the implementation details. Use this saving to an active record object, the way we are used to or not. It doesn't really matter. I just know what I can and need to do with this class and whether it writes to a database in the normal way or doesn't write to a database at all or draws it in crayon using a robot arm.

It's not really relevant to how I'm going to use it. It is taking whatever the idea of persistence is for this particular object and doing that. But that's still not even really including any of the active model. That is kind of like following the active model and active record API. The other things that I'm using in this are attributes and validations.

Those are the ones that I find that I'm using most often because attributes sets up all of that nice instantiation where you just the name of the attribute and then pass a new thing. After model will take care of a significant instance variable and everything. Then the validation is if we're going to do a persistence, we want to make sure that we have something that's going to work or we want to have nice usable Rails-E type errors, if not. That's what you get with the validation to get all the validators in there. That's what the presence is.

It's all of those things that you might be using everywhere else. You can all see those on your non-active record models. I'm now trying to remember what bits of this I took advantage of when I used it before. By the time we're on reflection, I wish I had used active model. I was building what I think I thought was going to be just a little one-off Ruby only app that gets some data for us from this third party database that we have permission to query but not right to.

We could write to it but only certain tables. It was a weird situation. I started out making plain old Ruby objects and using them to get this data and then I'll go send it over to this other place where whole Rails app will use it. When I got to testing, I realized, oh my god, this is awful. Now I have to write all of these little helper things myself.

I couldn't use a lot of the testing gems that I'm used to, the way that I'm used to using them, but I went to make set up some fake data. Factory Bot was no longer just there and waiting and excited for me. I'm going to say this is many years ago and I've learned lessons. I would like to think I would approach this differently from the beginning now. As I went through and started trying to test things and set up fake data and validate stuff, I realized that all of the things I love and use about active model all the time are really hard to reproduce on your own and why would you?

Right. I wish that you had told me active model everywhere like four years ago. I can't imagine why you didn't. Yeah, I'm sorry about that. I'm just kind of saying we don't have our own database.

This is just a little like let's fetch some data, squish it a little bit and bring it back. We don't need all of that. And then it's kind of like, well, but you've got several classes that are just trying to represent a real world concept as a collection of data. Why wouldn't you use active model? That's kind of what that is.

Yeah, that's a little bit along the lines of I know there are a lot of projects out there that include active support, but aren't using Rails because they want all of those syntactic sugar and kind of helpers and things that you have in Rails that are honestly those are the parts that are hard to tell apart from Ruby sometimes. Yeah. Yeah. What's an active support and what's in the standard library. But yeah, these little modular bits of Rails that you can include outside of Rails application to get a lot of this functionality that you're using day to day.

Yeah. And I think really a way that I think about Rails very often is like, I think you can pick and choose bits from. Mm-hmm. I think it is like, okay, it's this framework and we're going to use it or we're not going to use it. And usually we're going to use it.

But really the more I learn about it and the more I get into details realizing that you can just like extract the bits that you need and that they're all actually packaged up pretty nicely for you to do that. Yeah. And that it doesn't have to be full on Rails or just plain old Ruby. It can be like Ruby plus active support, Ruby plus active model. Yeah.

That is also for me not a way that I usually think about Rails. There was something I'm doing again. Are you about to talk about keynote? Yeah. Yeah.

There was one of Eileen's, you know, it's where she was describing the architecture of Rails and talking about these different parts as being little units that all kind of get piled up together and work together. And that was where I learned the difference between active something and action something. Oh, I actually don't know that difference. Would you tell us? Yes.

So if it's active something is going to happen more on the back end and it's action something is going to happen more on the front end. Wow. That, okay. I'm thinking through action mailer. That's kind of...

The action is going outside of Rails. It's sending a mail away. Yeah. So it's like a fast rule or a convention or just a pattern and, wow, what do you guys think of those things now? Yeah.

I don't know that if there's anything hard and fast like written down that defines that, but I do know that it is something that the Rails team is thinking of. It's certainly a convention at the very least. This was also where she had described active and action and then a few minutes later in the talk was talking about being more involved in Rails and the community and she says you can take an active role. And that's when I wanted to make a jam and add it to Rails that would do roles and permissioning. I don't think I'm going to get around to that.

So that is an idea I'm throwing out there for free. If you make it, we're going to make you come talk about it with us. Oh, there you go. Yeah. I'm going to tie this back.

This makes me think about the way that I built my sweater. Oh, okay. Right. Is that Rails has all these bits and pieces and you can use the whole thing as like just to here is a predefined pattern, make the sweater. Or you can take the bits you need and put them together in the order that you need and make it pretty and make it be exactly what you need it to be.

Look at that. Yeah. Well done. Crafting comes back again. Yeah, that's really fantastic.

I guess we think about Rails as being monolithic because your average web application is going to use a lot of, if not most of those parts kind of just in the process of being a web application. Yeah. So we don't have to pick and choose very often. But you can bring things in where they aren't already and get more use out of them by bringing active model. So I should have done this like an episode of the tightly coupled podcast where I read through the guides of active model beforehand.

So I have more of these specifics to talk about, but there are things like callbacks and attributes. We talked about validators, come in active model API. There's like dirty checking, all of these different things that are pieces of Rails models that you can pull in as needed. And so you don't even need to pull in everything to the class that you're making, just the one part that you're going to be making use of. Serializers.

That's another one too. So we're doing active model everywhere. What else could we be doing in more places than we are? Well, we talked a little bit about active support outside of Rails. You can certainly use active record on its own to work with a database, but that's not taking a piece of Rails and using it elsewhere in Rails.

I'm looking at the Rails Guide Index. Hoping that there would be another place where it would be like, here's a thing you can do. Yeah, I don't know that I've reached around too much because I feel like at the moment, at least active model kind of scratches all the different itches for functionality that I've needed. And thinking about these objects as resources, as models, even if they're not backed by a database table, you can overwrite certain methods that get clued off on to say include them in views, like to partial path, the overwrite, or make from scratch. And you can have your object work with your views as a partial in the same way that any other typical Rails model object would.

And I find that much like staying close to Rails's conventions as far as the way you're going to implement something or the methods that you might choose to use going day to day, keeping the same sort of similar API of model view and controller, keeping those consistent and Rails-y all the way through makes reasoning about your application a lot easier because you're not having to remember all of these different special cases. Yeah. So I think it's like Rails all the way up and down. This is making me think about something that I've been thinking about recently is the number of things that I don't really know where they're from and I just use them. I've been doing a lot of React and JavaScript recently, as I may or may not have mentioned.

And so when you're building an application there and you want to reach for something that's not in the standard library, you've got to know where it's coming from and import it explicitly. And there's so much in Rails and in Ruby that I just use without thinking about where it comes from. And I don't actually know, is this a Ruby thing? Is this a Rails thing? Is this active model?

I just kind of take for granted that those tools will be there and don't think very hard about where they come from. But I've been kind of wanting to get a better understanding of when I'm reaching for something, where am I reaching for it? Where's it coming from? Is this a Ruby thing? Is this a Rails thing?

Is this a gem that I use so often that I install it on every project that I've forgotten? It's not actually Rails. And I feel like part of the Rails magic is that you don't have to know that. But the more time I send in Rails, the more I do want to know. Do you ever wonder about that?

Do you have a better grasp of that than I do? I think this kind of falls into the similar situation to most everything that I've learned about developing software is I know the ones that I've had to dig into to make something work, but just kind of recreationally exploring what's out there. I've not done it as much. And that's something that I think Rails sort of encourages in a way too, right? You don't have to know how this works as long as the standard thing is going to get your work done for you.

And then you can dive more deeply when you have the need for it, right? That's part of Rails magic, Rails productivity. So no, I know a couple of them, but knowing all of the ways that the sort of tendrils of these little packages work together, that kind of greater subway metro map of Rails is not something that I have a whole lot of light on. I like the idea of thinking of it as a subway map. Yeah.

Just like visualizing all these things come together. I was just thinking, oh, this might make like a fun little game where you have to like, you know, here's a method where a class that you use all the time. Do you know where it comes from? Interesting. Fun or I don't know, my definition might be weird.

Well, we do have our end of the year thought by hackathon coming up. So maybe this is something that someone will want to put together and release. And if that does happen, we'll talk about it again. Yeah, absolutely. There was a little game like that when I was an early career dev that was asking, it would give you a list of 10 methods and would ask is this from Ruby or is this from Rails?

And myself and a bunch of other early career devs took it and, you know, did not really well because we haven't had to dig into like where did these come from very much? And a manager I had that was a really fantastic guy, but he came over and sort of pointed and laughed at all of us because he had gotten a better score just by randomly guessing true or false or like Ruby or Rails than the people he didn't know Ruby or Rails. And so he got a better score than the rest of us because we were overthinking everything. Man, I hope that still exists and you can send me the link and we can share with everyone else. I will.

Yes, I will try to find that. Yeah. That's pretty much exactly what I was thinking is this Ruby or is this Rails, but you know, maybe more end up. And then there's some things that are both right, like you can, I hope I'm not making this up, there are some methods that are defined in Ruby and then defined differently in a part of Rails. I don't know any off the top of my head that doesn't mean they don't exist.

Okay. Thought experiment. Homework for listeners. Do those exist? Share them with us.

Do they not exist and I'm ridiculous for thinking they are. Share that with us too, but nicely. Is there something super obvious that you're all shouting at your podcast player right now that we can't think of? Yeah. I would say in my defense, and I don't know if you have this audio.

I know that you don't. It is Monday morning for me. So I'm actually still carrying out. You over there and Scotland have had a whole data. Remember everything you've never known about programming.

Right. Because that's how that works. Well, shall we wrap it up? Yeah, let's do it. Let's go put active model some places outside of the podcast.

I'm going to put active model my next player. Amazing. Can't wait.

Frequently asked questions

How long is this episode of The Bike Shed?

This episode is 27 minutes long.

When was this The Bike Shed episode published?

This episode was published on December 23, 2025.

Is there a transcript available for this episode?

Yes, a full transcript is available for this episode. You can read the complete transcript on the episode page.

Can I download this The Bike Shed episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.