Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1094901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:02:23+00:00 2026-05-17T00:02:23+00:00

Greetings, all, I’m working on an application in Ruby on Rails where we need

  • 0

Greetings, all,

I’m working on an application in Ruby on Rails where we need to keep track of a bunch of external services for each user (for example, Facebook, MySpace, Google, SalesForce, Twitter, WordPress, etc) that the app will access on behalf of the user. For some services, we will need to store an (encrypted) username and password, for some we will need to save OAuth data, for some OAuth2 data, and so on. As the app grows, we will need to support even more types of accounts, each with its own set of authentication data.

Each user can create posts within the application, and we will take those posts and send them to the external services to be published for the user. We then track the response to the published post (retweets on Twitter, likes/shares on Facebook, and so on).

So:

class User < ActiveRecord::Base
  has_many :services
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :publishes
end

class Publish < ActiveRecord::Base
  has_one :service
  belongs_to :post
end

class Service < ActiveRecord::Base
  belongs_to :user
  belongs_to :publish
end

I am debating between using Single-Table Inheritance for my Service types (for example, WordPressService, FacebookService, TwitterService, and simply serializing a simple hash to save the authentication data) and using a traditional, normalized scheme where every type of service is its own model and table. I would like to be able to easily iterate over all the Services associated with a User, and a Publish needs to be able to be associated to any type of Service (a Publish might be sent to WordPress, Facebook, or Twitter, for example).

Can I achieve that kind of model relationship using a traditional normalized approach? Or is this exactly what STI was meant to solve?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:02:24+00:00Added an answer on May 17, 2026 at 12:02 am

    Though I’m still not sure if it’s the “right” way to solve this problem, I’ve decided on using Single-Table Inheritance so that I can easily get a list of all the services that another model has_many of (since each subclass of Service is also a Service, I can call model_instance.services to get them all).

    To solve the problem of code duplication, I created a module for use in any model that should has_many :services as well as each type of service:

    module HasServices
      extend ActiveSupport::Concern
      included do
        has_many :services
        has_many :facebook_services
        has_many :twitter_services
        has_many :wordpress_services
      end
    end
    

    Service is also aware of its subclasses, so that menus, etc. can easily be created:

    class Service < ActiveRecord::Base
    
      @child_classes = []
    
      ...
    
      protected
    
        def self.inherited(child)
          @child_classes << child
          super
        end
    
        def self.child_classes
          @child_classes
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.