<commit_before>---
layout: post
title: Rails STI
categories: technical
---
Not everyday that you would run into STI (Single Table Inheritance). I've been setting up shipping and billing addresses for my User model in the recent project I'm working on and STI provides the perfect solution. However, there's a little trick that seem to be missed by many STI articles.

Setting up STI is easy. First, add a 'type' column to your model with migration. Then, inherit the parent class to the subclasses.

By default, the column name should be 'type', but it can also be anything you want. If you decided to not use 'type', you would need to add this line of code: `self.inheritance_column = :your_column_name`, in the parent class.

Easy enough, right? Now comes the tricky part. You CANNOT have the subclassess in the same file as the parent class. Otherwise you will encounter an `uninitialized constant` error.

The solution is simple:

For example, my parent class is `Address`, and the STI subclasses are: `ShippingAddress`, and `BillingAddress`. You would need to create separate files, according to naming conventions, in models folder for those subclasses, and put those empty subclasses in. So I would need a `shipping_address.rb` for my `ShippingAddress` sublcass and a `billing_address.rb` for my `BillingAddress` subclass.

Much thanks to this [issue](https://github.com/sferik/rails_admin/issues/361)<commit_msg>minor fix<commit_after>---
layout: post
title: Rails STI
categories: technical
---
Not everyday that you would run into STI (Single Table Inheritance). I've been setting up shipping and billing addresses for my User model in the recent project I'm working on and STI provides the perfect solution. However, there's a little trick that seem to be missed by many STI articles.

Setting up STI is easy. First, add a 'type' column to your model with migration. Then, inherit the parent class to the subclasses.

By default, the column name should be 'type', but it can also be anything you want. If you decided to not use 'type', you would need to add this line of code: `self.inheritance_column = :your_column_name`, in the parent class.

Easy enough, right? Now comes the tricky part. You CANNOT have the subclassess in the same file as the parent class. Otherwise you will encounter an `uninitialized constant` error.

The solution is simple:

For example, my parent class is `Address`, and the STI subclasses are: `ShippingAddress`, and `BillingAddress`. You would need to create separate files, according to naming conventions, in models folder for those subclasses, and put those empty subclasses in. So I would need a `shipping_address.rb` for my `ShippingAddress` sublcass and a `billing_address.rb` for my `BillingAddress` subclass.

Much thanks to this [issue](https://github.com/sferik/rails_admin/issues/361).
{'fraction_non_alphanumeric': 0.06379978850898836, 'fraction_numerical': 0.0021149101163200562, 'mean_word_length': 4.664670658682635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3804565', 'n_tokens_mistral': 757, 'n_tokens_neox': 724, 'n_words': 426}