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

  • SEARCH
  • Home
  • 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 7582725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:30:22+00:00 2026-05-30T18:30:22+00:00

Is it possible to implement, in Rails 3, an association class ApiCredentials < ActiveRecord::Base

  • 0

Is it possible to implement, in Rails 3, an association

class ApiCredentials < ActiveRecord::Base
  belongs_to Model

end

where Model can be any model.

Then in the api_credentials table, I have fields like model_id and model to track this association.

I don’t assume that the convenient standard ActiveRecord queries will still work, but has anyone implemented something like this efficiently or used another method?

In the end, all I want to do is still to check if a particular model has ApiCredentials but I cannot know in advance whether the model will implement it.

Thanks in advance.

  • 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-30T18:30:23+00:00Added an answer on May 30, 2026 at 6:30 pm

    Sounds like you’re talking about a polymorphic association:

    With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model.
    […]
    You can think of a polymorphic belongs_to declaration as setting up an interface that any other model can use.

    This even uses your model_id/model columns but calls them X_id and X_type where X is the association name:

    class CreatePictures < ActiveRecord::Migration
      def change
        create_table :pictures do |t|
          #...
          t.integer :imageable_id
          t.string  :imageable_type
          #...
        end
      end
    end
    

    In your case, you’d have something like this:

    class ApiCredential < ActiveRecord::Base
      belongs_to :creditable, :polymorphic => true
    end
    
    class Pancake < ActiveRecord::Base
      has_many :api_credentials, :as => :creditable
    end
    
    class OneCentStamp < ActiveRecord::Base
      has_many :api_credentials, :as => :creditable
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to implement a forgot password using ASP.Net membership provider? Any sample
Is it possible to implement the model–view–controller pattern in Java for Android? Or is
I was wondering is it possible to implement a graph into rails application that
Possible Duplicate: Implement linked list in php We can make link list in Java,
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
Is it possible to implement autoboxing for your own classes? To illustrate my example,
Is it possible to implement Bluetooth devices to provide conference rather than one-to-one operation.
Is it possible to implement batching of multiple stored procedure calls (doing updates/deletes) in
Is it possible to implement McCarthy's amb -operator for non-deterministic choice in C#? Apparently
It's possible to implement INotifyCollectionChanged or other interface like IObservable to enable to bind

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.