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 6323843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:35:45+00:00 2026-05-24T16:35:45+00:00

I’m trying to wrap my head around how to set up this schema with

  • 0

I’m trying to wrap my head around how to set up this schema with polymorphic associations:

a “Document” has one metadata object, but this can either be “PDFMetaData” or “TXTMetaData”.

My concerns are:

To set up this association, I can do this

class Document
  belongs_to :metadata, :polymorphic => true
end

class PDFMetaData
  has_one :document, :as => :metadata
end

class TXTMetaData
  has_one :document, :as => :metadata
end

This works, but it kind of feels like reverse for me: A document has_one metadata object, not opposite?

Also, I’m really running into problems when trying to create a nested form for my new document. I know I can use fields_for, but how do I know what kind of object it is? (PDFMetaData or TXTMetaData). Do I have to render separate partials depending on what kind of document I have?

I’m afraid that the latter ties in with my first question, and that I’m doing something terribly wrong.

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-24T16:35:46+00:00Added an answer on May 24, 2026 at 4:35 pm

    Although I can see where you’re coming from about the confusion with arrangement of belongs_to & has_one in this case, the rationale is based on where the foreign_key is in the DB schema. The documents table contains the foreign_key that relates it to the PdfMetaData object thus it wouldn’t make sense to describe the relationship the other way round since there would be no way to have a has_many relationship (how would you store multiple foreign keys in the 1 database row?)

    Hopefully that makes sense … but on to the 2nd problem, the solution depends on the list of valid attributes on your TXTMetaData and PDFMetaData classes. If they have the same attributes (or at least the ones you want available in your form are the same) then you should be okay with

    fields_for :metadata do |meta_fields|
      meta_fields.text_field :attr
      meta_fields.text_fields :attr2
    end
    

    and so on

    If however you want to expose different attributes then I’d suggest detecting the class of your metadata object and act accordingly e.g.

    meta_obj = document.metadata
    fields_for :metadata, meta_obj do |meta_fields|
      if meta_obj.is_a?(PDFMetaData)
        meta_fields.text_field :attr
        meta_fields.text_fiels :attr2
      elsif meta_obj.is_a?(TXTMetaData)
        meta_fields.text_field :other_attr
        meta_fields.text_fiels :other_attr2
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.