Web Development

Summarized using AI

Candy on Rails: Polymorphism & Rails 5

Michael Cain • April 17, 2018 • Pittsburgh, PA

The video titled "Candy on Rails: Polymorphism & Rails 5" features speaker Michael Cain from RailsConf 2018, focusing on the use of polymorphism in Ruby on Rails applications. The presentation provides a foundational understanding of polymorphism and its practical applications, especially within a candy shop application context.

Key Points Discussed:

- Introduction to Polymorphism:

- Polymorphism refers to a type of association that allows multiple models to be linked by a single association, simplifying complex relationships.

- The principle emphasizes the flexibility and abstraction it provides, reducing coupling in code.

- Real-World Application Example:

- Using a candy shop application, Cain illustrates how a goodie (product) can have various packaging types that can evolve dynamically.

- Highlights how the polymorphic association allows for multiple types of packaging (like bags, boxes, etc.) under one general model without hardcoded references.

- Benefits of Polymorphism:

- Simplifies code maintenance and scaling by isolating complexity within a single association model.

- Reduces the need for multiple foreign key associations, which can complicate the database schema.

- Challenges and Considerations:

- While polymorphism offers many benefits, it is essential to use it judiciously to avoid masking underlying problems in the application architecture.

- Developers should evaluate whether a new model is original enough to justify a polymorphic association rather than creating unnecessary complexity.

- Best Practices:

- It's crucial to apply polymorphism only when there are sufficient distinct models or attributes that require this level of abstraction.

- Assessment of user needs is vital; if packaging details are not essential for the end-user, simpler solutions like using JSON objects could suffice.

Conclusion and Takeaways:

- Polymorphism can be a powerful tool for managing complex relationships in Rails applications, but it should be employed carefully.

- Always keep the user perspective in mind and assess whether abstractions genuinely improve the usability and maintainability of the system.

- The session ends with a reminder to balance abstraction with practicality to ensure clear, maintainable code.

Candy on Rails: Polymorphism & Rails 5
Michael Cain • April 17, 2018 • Pittsburgh, PA

RailsConf 2018: Candy on Rails: Polymorphism & Rails 5 by Michael Cain

Polymorphism is a mainstay of the Ruby on Rails stack, affording us a lean, concise way of relating objects in a dynamic way. Using a candy shop application, this presentation will pragmatically explain and demonstrate polymorphism and its benefits/usefulness in Rails.

RailsConf 2018

00:00:11.330 Welcome to Polymorphism in Rails 5.
00:00:15.540 I'm Michael Cain. If you're interested in connecting with me on Twitter, my handle is Code Train.
00:00:20.850 I was a musician for a while, so if there are any jazz heads in the room, we’ll address the elephant in the room.
00:00:26.660 Of course, this is Michael Cain. I can do a pretty good impression, so I'll see if I can bust that out later.
00:00:33.809 So, what's the scope of this talk? How many people in this room have been developing Ruby on Rails for a year or less? One, two, three years? Okay, more than three hundred. This is probably gonna be a review for you, but for everybody that's new, we’re going to take a fundamental look at how Rails utilizes polymorphism and find some practical examples where it is most prudent to use it.
00:00:48.180 The goal is that when you leave here, you'll have an introductory understanding of how polymorphism works, along with a practical use case. You'll understand when this tool is beneficial and when it may not be.
00:01:07.650 And I know people have stickers to give away, but I've got a hundred bags of free candy since we’re talking about candy. Please come up and get one afterward. Who doesn't like free candy, right? So, what we're gonna cover in terms of polymorphism is its significance in the Ruby on Rails stack.
00:01:37.770 Of course, we'll be using our candy shop application to discuss the practical applications of polymorphism and when it’s appropriate to use it. We’ll also cover some complex concepts beyond standard polymorphism as we progress.
00:01:55.680 So, what is it? Polymorphism does not just exist within the confines of software development. In short, it means that one thing can have multiple appearances. For example, in biology, polymorphism is when a species can possess multiple distinct appearances.
00:02:09.060 For instance, there are species of snails that have very different patterns on their shells, or species of jaguars that have light and dark fur. They are the same species, but they look entirely different.
00:02:30.390 In relation to our context, polymorphism refers to a type of association where multiple models can belong to a single association. Essentially, multiple things can be expressed through one association.
00:02:44.310 So, why is this a powerful tool? Why would you want to use this? We often refer to 'convention over configuration,' which has served us well over the years. You'll hear a lot about the single responsibility principle or SOLID principles, emphasizing that simple code is good code.
00:02:56.550 Polymorphism frees us from the complexities of tightly coupled components. The idea is that you can have many complex relationships on one side and manage them through a simple interface.
00:03:11.140 It’s also very 'DRY' - 'Don't Repeat Yourself,' as you’ll see later. As our complexity evolves, we can isolate that through a polymorphic association instead of dealing with increasingly complex packaging changes.
00:03:24.960 Now, let’s talk a little bit about our new business: Candy on Rails, a sugar-as-a-service platform. Our application is a web service that allows customers to order handmade confections made by our open-source chefs.
00:03:43.600 We’re putting our recipes out there for anyone who wants to get involved and actually make the product and send it out locally. We refer to this as our distributed kitchen system.
00:04:00.570 As I mentioned, our recipes are open-source, and our open-source chefs can make tweaks and update the recipes. For instance, we might use an impression of Michael Cane inspired toffee, combined with rich chocolate, or we might create Italian shortbread with a hint of anise and a maple glaze.
00:04:40.230 Similarly, we could have cookies inspired by different figures in our community. This is to highlight how collaborative our approach is, and I hope you all realize I’m glad to make jokes about my impression.
00:04:59.170 Now, since our goodie makers are distributed, we need to keep track of who is making what because, ideally, people want to get paid, right? The goody itself is the object of interest for our business. Below you'll see some of our standard packages, and they are quite self-explanatory.
00:05:24.560 They vary in size, and we plan to expand our offerings in the future to include themes for holidays or birthdays, even allowing for custom designs.
00:05:43.990 Here is a UML diagram for the backend of our application. It visually represents our goodies, including names, descriptions, who made them, and contributors. In the middle, we see different types of packaging: bags, gift boxes, and greeting cards. All of these are connected to an order containing customer details.
00:06:00.670 However, there are potential issues with this structure. For instance, a goodie can only belong to one container, which can lead to nil values if we have multiple relationships.
00:06:45.000 Every package requires its own ID, so managing those can become cumbersome. For instance, if we create five new holiday packages, we'd have to manage five new IDs, complicating our associations. This is where polymorphism comes in, allowing us to streamline associations.
00:07:31.820 With polymorphism, we can dynamically associate items, as it incorporates both the 'what' and the 'whom.' Instead of explicitly defining IDs, polymorphism allows us to declare dynamic tables, letting our code know what we need beforehand.
00:08:26.490 This creates a cleaner association process. The goody model doesn't need to know specifics about the package—it simply knows it needs a particular type with a corresponding ID.
00:09:06.600 So how does this work at the SQL level? While you don't need to touch this detail, if you're curious, every polymorphic association has an ID and a type that specifies what model we're dealing with.
00:09:42.400 Understanding when it's prudent to use polymorphism is crucial. Like many aspects of Rails, it can be a powerful tool in the right hands. However, overusing it can complicate matters.
00:10:30.570 Consider whether your polymorphic association is solving a problem or masking one. For example, if your packages don't have unique attributes to warrant individual models, consolidating this data into a settings JSON object may be more effective.
00:11:02.190 As business needs evolve, you need to ensure your associations are clear and simple. Let’s say our customers want more details about the goodies we provide—especially concerning allergens.
00:11:41.800 For instance, children have allergies, and a cookie that might look appealing could actually be dangerous. If our goodie models are unique enough, they could justify incorporating polymorphism.
00:12:08.000 Here's how one would handle an optional parameter in a 'belongs to' association. In Rails 5, by default, 'belongs to' is strict, ensuring an ID exists. If not all goodies need their IDs, we can make this optional, preventing potential errors.
00:12:39.800 This flexibility in defining associations is crucial for maintaining code integrity. However, if this structure grows cumbersome, consider creating a concern to encapsulate complex association logic.
00:13:30.190 This allows for efficient management without cluttering our models. We want to maintain clear pathways for how our packaging data interacts without overwhelming the individual models.
00:14:26.460 In conclusion, polymorphism is a useful means of abstracting complexity within our associations. As packaging evolves, isolating changes helps ensure our core models remain stable.
00:15:00.490 Remember, abstraction can be dangerous if not handled properly. It can make things seemingly complex and hard to manage, so always question whether polymorphism is the best solution.
00:15:30.260 Ultimately, it’s about serving our users effectively. Thank you very much.
Explore all talks recorded at RailsConf 2018
+98