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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:22:18+00:00 2026-05-13T12:22:18+00:00

I have a model representing a Content item that contains some images. The number

  • 0

I have a model representing a Content item that contains some images. The number of images are fixed as these image references are very specific to the content. For example, the Content model refers to the Image model twice (profile image, and background image). I am trying to avoid a generic has_many, and sticking to multiple has_one‘s. The current database structure looks like:

contents
  - id:integer
  - integer:profile_image_id
  - integer:background_image_id

images
  - integer:id
  - string:filename
  - integer:content_id

I just can’t figure out how to setup the associations correctly here. The Content model could contain two belongs_to references to an Image, but that doesn’t seem semantically right cause ideally an image belongs to the content, or in other words, the content has two images.

This is the best I could think of (by breaking the semantics):

class Content
  belongs_to :profile_image, :class_name => 'Image', :foreign_key => 'profile_image_id'
  belongs_to :background_image, :class_name => 'Image', :foreign_key => 'background_image_id'
end

Am I way off, and there a better way to achieve this association?

  • 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-13T12:22:19+00:00Added an answer on May 13, 2026 at 12:22 pm

    The simple answer is to setup your associations in reverse of what you have, like so:

    # app/models/content.rb
    class Content < ActiveRecord::Base
      has_one :profile_image, :class_name => 'Image'
      has_one :background_image, :class_name => 'Image'
    end
    
    # app/models/image.rb
    class Image < ActiveRecord::Base
      belongs_to :content
    end
    

    You don’t need the foreign keys ‘background_image_id’ and ‘profile_image_id’ in the content table at all.

    However, there’s a more elegant solution: single table inheritance. Set it up now in case you want background and profile images to behave even slightly differently in the future, plus it will clarify your code today.

    First, add a column to your images table called type:

    # command line
    script/generate migration AddTypeToImages type:string
    rake db:migrate
    

    Now setup your models like this:

    # app/models/content.rb
    class Content < ActiveRecord::Base
      has_one :profile_image
      has_one :background_image
    end
    
    # app/models/image.rb
    class Image < ActiveRecord::Base
      belongs_to :content
    end
    
    # app/models/background_image.rb
    class BackgroundImage < Image
      # background image specific code here
    end
    
    # app/models/profile_image.rb
    class ProfileImage < Image
      # profile image specific code here
    end
    

    Now you can do all kinds of things like getting a list of all background images:

    # script/console
    BackgroundImage.all
    

    This is more true to the data model you’re trying to create, allows the easiest expandability in the future, and gives you some cool new methods today.

    UPDATE:

    I’ve since created a blog article called Single-Table Inheritance with Tests that goes into more detail, and covers testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that contains a boolean field representing the item's approval or
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
I have model Article it has field title with some text that may contain
I have a CoreData model with 2 objects. The first object (Images) contains a
In my Rails 3.2.6 app, I have a model representing a collection of data
I have a django model and a field representing a users full name. My
I have a mongoose model in my node.js application, representing invoices. I have figured
I have the model representing the player's ship gradually leaning when the player strafes.
In my WPF application data model I have multiple classes representing data for interaction
We have some data which represents many model runs under different scenarios. For a

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.