background preloader


Facebook Twitter

Doubly Polymorphic Join Models, for fun and profit · GitHub. Advanced ActiveRecord Recipes: Doubly polymorphic join models - Quick Left. When faced with complex data structures, fully denormalizing your database can be just as difficult as it is important.

Advanced ActiveRecord Recipes: Doubly polymorphic join models - Quick Left

Recently, I came across the challenge of building a join model (has_many :through =>) association that has double polymorphism. That is to say, all models that are joined by this table can connect to each other in some way, to create a mesh. This is better represented in art: Every delivery object relates to users as an impression, which ends up being a fancy way to store flags relating to the user and that delivery, including read status, and provides an easy way to give global read counts through the impression model. Double polymorphism is an edge case in itself, and adding a join model onto it adds its own variety of challenges to access objects. Here is the background code, with unnecessary assocations and pieces removed.

You also quickly begin to see where daisy chaining through nested has_many :through => operators becomes critical. That said, enjoy! A Polymorphic Join Table - Aaron Van Bokhoven. Here's an interesting problem I ran into today.

A Polymorphic Join Table - Aaron Van Bokhoven

Polymorphic associations in Ruby on Rails are actually quite easy to do, especially in Rails 3.2. If you need a refresher, there's a great screencast over at Railscasts, which does require a subscription which I highly highly recommend: However, my problem was a little different, and maybe a special case because I can't think of many applications this would apply to. Say I have a Location and a Checkpoint, and the Location and Checkpoint can have notes, posted by Users. I would use a polymorphic association for the Notes to the Location and Checkpoint. Example, two Locations which are near each other, maybe they're coordinates, could share a single Note describing the general area, and with the same Note model, one Note may describe multiple Checkpoints. Solution: Make the join table polymorphic. Ruby on rails - how to generate migration to make references polymorphic. Understanding Polymorphic Associations in Rails.

Polymorphic associations can be tricky to understand and many times one can get confused about when to use them, how to use them and how they are different from other associations.

Understanding Polymorphic Associations in Rails

In this article we will go over these issues. Introduction Active Record Associations are one of the most important features of Rails. Polymorphic association is part of these associations. Struggling with complex Rails activerecord associations and join models. Arrays - Rails 4: Saving multiple instances of same model in a single form. Chapter 10: Multiple Table Inheritance. Back to Table of Contents.

Chapter 10: Multiple Table Inheritance

We’ve spent a lot of time on the idea of tweaking your data model until it’s rock solid, impervious to application-layer bugs or a meddling Martha at the psql prompt. By now you should feel like you’re ready to practice referential integrity jujitsu or constraint kung fu. It may come as a surprise, then, to learn that there is a feature of ActiveRecord—polymorphic associations—that depends on breaking the referential integrity we’ve worked so hard to ensure.

Before you get your knickers in a knot, remember that ActiveRecord was primarily developed against MySQL, at a time when referential integrity was a “feature” that MySQL did not support. So it’s understandable that some “features” crept into ActiveRecord and Rails that are not really features at all, but disasters waiting to happen. The Problem Polymorphic association allows you to define a relationship between two tables without knowing ahead of time what one of the tables in the relationship is. Exercises. Ruby on rails - Polymorphic Association with multiple associations on the same model. Single Table Inheritance - Problems and solutions. As a consulting agency we are often asked to help with projects which embrace lots of typical Rails conventions.

Single Table Inheritance - Problems and solutions

One of the most common of them is the usage of STI (Single Table Inheritance). It is even considered best practice by some people for some use cases [YMMV]. I would like to show some typical problems related to STI usage and propose different solutions and perhaps workarounds. Story It is very common for US-related projects to store customer billing and shipping address.

Note Remember that this code is purely for demonstration of problems and solutions. Starting point Let’s say that your user can have multiple addresses. How (and When) to Use Single Table Inheritance in Rails - eugenius. Last week as I was developing an application to track and analyze ticket and event postings, I came across a design problem.

How (and When) to Use Single Table Inheritance in Rails - eugenius

I had first started working on my ticket tracker application as a way to parallel the learning that we were doing in class. I started with a simple scraper that pulled data out of the Stubhub’s client-facing HTML documents, then moved on to writing a simple ORM that saved the data to a SQLite database, then moved the application into Sinatra and finally into Rails. Along the way, as I was increasing the level of structural complexity, I was also expanding the scope of the project. I had initially started with just NBA tickets, but I’ve recently started exploring how to broaden the application to any event. This presented me with a design conundrum.