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.
Sounds like you’re talking about a polymorphic association:
This even uses your
model_id/modelcolumns but calls themX_idandX_typewhereXis the association name:In your case, you’d have something like this: