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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:43:19+00:00 2026-05-27T03:43:19+00:00

I have this table which contains a field called ‘icon_for’ which I have been

  • 0

enter image description here

I have this table which contains a field called ‘icon_for’ which I have been using to check if the icon can be used for a ‘User’ or a ‘Greeting’.

This is obviously not the best way to do this as if I want to use an icon for say both a ‘Greeting’ and a ‘User’ or even some other object I would be stuck.

Do I need to make another table called icons_object and then list the icon_id and object_name?
Seems like I would have to do a lot of repeating of the icon_id’s if an icon is used for several objects but is this the way to go?

Also what would this table association in Rails be. Say the Table is called icons_objects.
Then in Rails would it be

Icon.rb

icon has_many :icon_objects ?????

Totally confused here so any help for a NOOB would be appreciated.

Is there a way to do this normalized?

  • 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-27T03:43:19+00:00Added an answer on May 27, 2026 at 3:43 am

    You want to use polymorphic associations. For example:

    class Icon < ActiveRecord::Base
      belongs_to :iconable, :polymorphic => true
    end    
    
    class Greeting < ActiveRecord::Base
      has_many :icons, :as => :iconable
    end
    
    class User < ActiveRecord::Base
      has_many :icons, :as => :iconable
    end
    

    Then you could get to icons from greetings or user:

    @user.icons
    @greeting.icons
    @icon.iconable  #returns the parent type.
    

    Or if you wanted to query icons for a particular type:

    Icon.where(:iconable_type => "greeting")
    

    This approach is not unlike the one you have already taken. The benefits of doing this method is that you can get the parent object without worrying about what type it is, like you would if you tried to implement the other answer in this thread.

    You will just need to run some migrations to get everything working properly. You can see more information at http://guides.rubyonrails.org/association_basics.html#polymorphic-associations.

    Edit per comment:

    Strictly speaking, you’re correct. You cannot have a polymorphic record which points to a User and a Greeting. However, you can make a small change and create another model, which would kind of act like a join table.

    class IconConnection < ActiveRecord::Base
      belongs_to :inconable, :polymorphic => true
      belongs_to :icon
    end
    
    class Icon < ActiveRecord::Base
      has_many :icon_connections
    end
    
    class Greeting < ActiveRecord::Base
      has_many :icon_connections, :as => :iconable
      has_many :icons, :through => :icon_connections
    end
    
    class User < ActiveRecord::Base
      has_many :icon_connections, :as => :iconable
      has_many :icons, :through => :icon_connections
    end
    

    Each IconConnection would point to either a Greeting or a User, and each of the IconConnections would point to the same Icon.

    This setup assumes that you can have multiple icons for each user and greeting, but if not, you can replace the has_many‘s with has_one‘s

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

Sidebar

Related Questions

I have table called FinalForgotten which only contains one field called aname. The field
I have this mysql table called comments which looks like this: commentID parentID type
I have a field called PropertyValue in the UserProfile table that can contain information
I have mysql table called it as R which contains many fields, but I
I have table tblsale . In this table i have field called BillType ,
If you have a table called posts, which contains id, title, content .etc, how
I have a table called orders this table contains all the information we need.
I have a field in my table which contains categories data eg Computer >
We have a table with a key field, and another table which contains the
I have this table in an Oracle DB which has a primary key defined

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.