Artwork

Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Player FM - Podcast App
Go offline with the Player FM app!

Polymorphic Associations

9:23
 
Share
 

Archived series ("Inactive feed" status)

When? This feed was archived on July 14, 2021 01:41 (3y ago). Last successful fetch was on June 05, 2018 17:42 (6+ y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 150840411 series 1008985
Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Rails has the concept of Polymorphic associations, which are associations that can be of different data types. For example, let’s say we have a Comment model. A comment in your app can be on a post or a page. Rather than creating a PostComment model and a PageComment model, you can set up your Comment model to have a polymorphic association to a Page or a Post. Generally people call these associations “something-able”. In this case, it would be commentable. Here’s a quick code sample: class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Page < ActiveRecord::Base has_many :comments, :as => :commentable end class Post < ActiveRecord::Base has_many :comments, :as => :commentable end The only other thing you need to know is the database structure. On the database, you need two columns. A [polymorphic-association-name]_type string column and [polymorphic-association-name]_id integer column. In this case, it would be “commentable_type” and “commentable_id”.
  continue reading

33 episodes

Artwork
iconShare
 

Archived series ("Inactive feed" status)

When? This feed was archived on July 14, 2021 01:41 (3y ago). Last successful fetch was on June 05, 2018 17:42 (6+ y ago)

Why? Inactive feed status. Our servers were unable to retrieve a valid podcast feed for a sustained period.

What now? You might be able to find a more up-to-date version using the search function. This series will no longer be checked for updates. If you believe this to be in error, please check if the publisher's feed link below is valid and contact support to request the feed be restored or if you have any other concerns about this.

Manage episode 150840411 series 1008985
Content provided by Charles Max Wood. All podcast content including episodes, graphics, and podcast descriptions are uploaded and provided directly by Charles Max Wood or their podcast platform partner. If you believe someone is using your copyrighted work without your permission, you can follow the process outlined here https://player.fm/legal.
Rails has the concept of Polymorphic associations, which are associations that can be of different data types. For example, let’s say we have a Comment model. A comment in your app can be on a post or a page. Rather than creating a PostComment model and a PageComment model, you can set up your Comment model to have a polymorphic association to a Page or a Post. Generally people call these associations “something-able”. In this case, it would be commentable. Here’s a quick code sample: class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Page < ActiveRecord::Base has_many :comments, :as => :commentable end class Post < ActiveRecord::Base has_many :comments, :as => :commentable end The only other thing you need to know is the database structure. On the database, you need two columns. A [polymorphic-association-name]_type string column and [polymorphic-association-name]_id integer column. In this case, it would be “commentable_type” and “commentable_id”.
  continue reading

33 episodes

All episodes

×
 
Loading …

Welcome to Player FM!

Player FM is scanning the web for high-quality podcasts for you to enjoy right now. It's the best podcast app and works on Android, iPhone, and the web. Signup to sync subscriptions across devices.

 

Quick Reference Guide