activerecord - Rails: Single Record, Many Polymorphic -


i'm sure there better title this, sorry that.

i've got verification model. ideally, there various other model types able associate single verification record. instance.

  • verification(id: 1, verifiable_type: 'reference')
    • reference(id: 1, verification_id: 1)
    • reference(id: 2, verification_id: 1)
  • verification(id: 2, verifiable_type: 'degree')
    • degree(id: 1, verification_id: 2)
    • degree(id: 2, verification_id: 2)

i hoping simple dynamic :class_name option on the has_many:

class verification < activerecord::base   has_many :verifiables, class_name: -> { dynamic_class_name } end 

i 99% sure reverse associations work out of box. so, using records above, following should no problem:

class reference < activerecord::base   belongs_to :verification end  class degree < activerecord::base   belongs_to :verification end     

any suggestions?

instead of

 has_many :verifiables, class_name: -> { dynamic_class_name } 

replace with

 def self.varifiables(type)    where(verifiable_type: type)  end 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -