Forbes India Leader Recognition
e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Implement Getstream.io with STI in Rails

Implement Getstream.io with STI in Rails

Featured Image

What is Getstream.io?

What is the best way to interact with the live data in the application? Yes, the answer is through FEEDS or CHAT.

FEEDS or CHAT are the best way to be in touch with the people you care about.

However, the user expects the feeds to be more Relevant, Engaging and Fast.

Getting the relevant posts for a user by building a relational database is a lot more time consuming and difficult to maintain for large applications.

Gestream Activity Feeds is the best solution for building applications like Facebook, Instagram and Twitter.

Basically, any requirement where the user can follow things or people to get the best contents in which they are interested in is the best-fit Getstream.io

Getstream allows users to follow the topics or users of their interest and get the posts from them on their timeline.

What is STI – Single Table Inheritance?

When there is a case where you want to store the data into a single table but should have multiple subclasses sharing a single base class, then STI is the best fit for you.

In short, many subclasses inherit from a single base class storing the data in a single table. This is achieved with the help of the “Type” column in the base class.

We will have to add a type column in the base class, which tells rails that we are using STI. Any common validation we want to apply will be in the base class and the specific validations will be in a required subclass.

Implementing Getstream.io with STI

Let’s take a real-world example, where a user can create a post that can include an image, video, blog, audio etc. and all the posts will be stored in a single table called posts.

To achieve this, we will start with implementing STI for multiple types of posts and Getstream.io for getting the user feeds on the timeline.

First, we will start with creating a posts model and adding a type column to the posts table.

Our post model will look like this:

Implement Getstream.io with STI in Rails

Then we will create some subclasses for other types of posts like image, video, audio.

Implement Getstream.io with STI in Rails

The post table has a ‘type’ column and others are the subclasses derived from the post base class. Any common validation that we want to apply on post will be in post.rb and other post type-specific validations will be in the respective model.

We will have separate controllers and routes for creating all types of posts. You can define the routes like this:

Implement Getstream.io with STI in Rails

We are all set with our STI structure and now the posts table will have a column type that will indicate the type of post. This will happen when we insert data into the posts table using Photo.new() or Video.new or Audio.new()

This way the type column will have values like “Photo” or “Video” or “Audio” depending on the type of post we create.

Here we are able to create three types of posts having different models derived from a single base class i.e post.rb

In the next step, we will implement the Getstream.io to get the feeds using the Stream Rails gem.

Official site and documentation for Getstream:

Official site and documentation for Stream Rails gem:

First, we will have to follow the steps given in the link below this link.

  1. Install the stream_rails gem:

    gem install stream_rails
  2. Setting up the Getstream:
    Add a new file at config/initializers/stream_rails.rb with following content:
    Implement Getstream.io with STI in RailsTo get the api key and secret key, we will have to log in to https://getstream.io/ and get the credentials.
  3. Model configuration:
    Now as we are using STI for our posts, we have our base class called post.rb and instead of adding each post type to the stream, we will integrate our base class i.e post.rb with the stream.Add following code to post.rb

    include StreamRails::Activity
    
    as_activity
    
    def activity_object
    
        self
    
    end

    Our post model will look like this:
    Implement Getstream.io with STI in Rails

    Now whenever we create a post, it will get stored into the feeds for that user and will be deleted from feeds once the post is deleted.

Now we are all set with integrating Getstream.io with our STI implementation.

There are few important concepts of Activity feeds:

  1. Activity_object: The object of ActiveRecord model instance stored into the feed.
  2. Activity_actor: The user who created that post.
  3. Activity_verb: Model class name of the verb i.e Photo/Video/Audio.

We are able to insert into feeds whenever the post is created. But what about getting them back and displaying them on the user’s timeline?

Stream_rails provides the Feed Manager using which we can fetch the activities from the feeds.

There are various types of feeds present in the Feed Manager.

  1. User Feed:
    Stores all the activities of the current

    userfeed = StreamRails.feed_manager.get_user_feed(user.id)
  2. News Feed:
    Feeds from the people you follow.

    feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:flat]
    aggregated_feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:aggregated]
  3. Notification Feed:
    Feeds to build a notification system

    notification_feed = StreamRails.feed_manager.get_notification_feed(current_user.id)

This is how we can build the activity application with user feeds using the rails Single Table Inheritance and Getstream.io

Related Posts

Latest Posts

  • All Posts
  • AI Powered Knowledge
  • ai/ml
  • CEO India Magazine
  • CMMI level 5 Certification
  • e-learning
  • Fintech
  • gaming
  • Generative AI
  • healthcare
  • manufacturing
  • News
  • OTT
  • Portfolio
  • supply chain
  • travel and hospitality
  • Tudip's AI Hackathon
  • Voxlearn Enterprises
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python
The Future of Workplace Learning: AI-Powered Knowledge on Demand

The Future of Workplace Learning: AI-Powered Knowledge on Demand

June 12, 2026

A few months ago, I was sitting in a meeting with a team lead who looked genuinely frustrated. Not because…

Read More
We Did It Again: Tudip Successfully Renews Its CMMI Level 5 Certification

We Did It Again: Tudip Successfully Renews Its CMMI Level 5 Certification

June 9, 2026

Nobody around here needed a memo to know something worth celebrating had happened. The message from the CMMI Institute said…

Read More
CEO India Magazine Features Dipti Agrawal: The Woman Behind Intelligent Enterprise Solutions

CEO India Magazine Features Dipti Agrawal: The Woman Behind Intelligent Enterprise Solutions

June 9, 2026

There are moments at work that just stop you in your tracks. Not the big quarterly reviews or the product…

Read More

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Singapore

77 High Street, #10-12B High Street Plaza, Singapore 179433.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com