March 22, 2016 by Daniel P. Clark

Rails: Has One Through Polymorphic Relation

As I’ve described in my blog post “Rails Polymorphic Associations” I’ve stated that polymorphic relations in Rails are best for scenarios where a model may “belong to” one of many other kinds of model.  So a Comment model with the polymorphic commentable relationship can belong to any other record in your Rails app.  This is…

Continue Reading »

February 10, 2015 by Daniel P. Clark

Rails Polymorphic Associations

What is a polymorphic association you might ask?  It’s where an ActiveRecord model can potentially belong to more than one other kind of model record.  So the single instance of your car belongs to you, a person, whereas other cars may individually belong to a car lot, a business. Why do we need it? In…

Continue Reading »

August 1, 2014 by Daniel P. Clark

Manual Polymorphic Creation in Rails

Finding documentation online to do this was pretty much a no go.  The answer I’ve provided here was grasped from hints given on many websites of what could be done but without anything I could really sink my teeth into. What this code allows you to accomplish is to create a belongs_to/has_many relationship to something…

Continue Reading »