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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:30:45+00:00 2026-05-13T01:30:45+00:00

sorry if this question has been asked before, I searched but wasn’t sure on

  • 0

sorry if this question has been asked before, I searched but wasn’t sure on the proper name for what I’m looking for.

I’m pretty much a newcomer to rails and development in general, I’ve done some hacky programming but I wouldn’t say I know what I’m doing. I’ve always been at a loss on how to define “types” in my Rails models and in databases in general.

For example, say I have a model “Car” and it has the property “Colour”, where Colour is chosen from a known set rather than an RGB value or whatever. Something tells me that there should be another table (and associated model) for Colour and then have some kind of relationship between the two, but which relationship is appropriate? A car doesn’t BELONG to its colour, or vice versa.

Or of course I could just store an integer and look it up in code, but this feels wrong to me.

What did I miss? 🙂

  • 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-13T01:30:45+00:00Added an answer on May 13, 2026 at 1:30 am

    Color may be a complex object or may be a very simple one, depending on what you need.

    • Simple: Integer subsequently converted to any other format, or hex value like “a3ab34” stored in a string.
    • Complex: You want to map colors to certain human titles.

    For simple you’ll just have a column in cars named color, and override the reader method to return whichever color format you desire, like so.

    class Car < ActiveRecord::Base
      def color(format = :integer)
         case format
         when :integer: super
         when :hex: # ... hex conversion code
         # ... more conditions ...
         end
      end
    end
    

    If we’re talking about complex case, then it’s different. You should then have a separate model called Color. Then Car would belongs_to :color like thenduks said above. It would be more semantically correct to say Car has_one :color but then you’re forcing colors table to store car_id which would be kind of backwards. In our case, Car will be storing color_id. So make sure you make a migration that adds color_id to table cars.

    Then you should create the migration for colors table, which would include id, title and code for example, and which you’d have to pre-populate yourself using rails seeding stuff. (see http://railscasts.com/episodes/179-seed-data)

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

Sidebar

Related Questions

I'm sorry if this question has been asked before, but I'm not even sure
Sorry I know this question has probably been asked before, but do you know
So, if this question has been asked before, I'm sorry. I'm not exactly sure
I'm sorry if this question has been asked before, but I can't seem to
Sorry if this has been asked before or it's a really dumb question, but
I'm sorry if this question has been asked before, but the idea is hard
I'm sorry if this question has been asked already, but I couldn't find it
Sorry if this question has been asked before. On my search through SO I
I'm sorry if this questions has been asked before, but I couldn't find an
sorry if this questions is overly basic or has been asked before but I

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.