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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:16+00:00 2026-05-12T17:14:16+00:00

I am using Single Table Inheritance for managing different types of projects. I decided

  • 0

I am using Single Table Inheritance for managing different types of projects.
I decided to store some information associated with each project type. So i created new table “project_types” with “model_type” field as primary key. Primary key values are values of “type” field of “projects” table. Problem: When i trying to get associated with Project object ProjectTypes object it always returns null.

>> p = Project.find(:first)
=> #<SiteDesign id: 1, type: "SiteDesign", name: "1", description: "dddd", concept: "d", client_id: 40, created_at: "2009-10-15 08:17:45", updated_at: "2009-10-15 08:17:45">
>> p.project_type
=> nil

Getting projects associated with ProjectTypes project is OK. Is there way to make it works properly?

Models:

class Project < ActiveRecord::Base
    belongs_to :project_type, :class_name => "ProjectTypes", :foreign_key => "model_name"
end

class SiteDesign < Project
end

class TechDesign < Project
end

class ProjectTypes < ActiveRecord::Base
  self.primary_key = "model_name"
  has_many :projects, :class_name => "Project", :foreign_key => "type"
end

Migrations:

class CreateProjectTypes < ActiveRecord::Migration
  def self.up
    create_table :project_types, :id => false  do |t|
      t.string :model_name , :null => false
      t.string :name, :null => false
      t.text :description

      t.timestamps
    end

    add_index :project_types, :model_name, :unique => true


    #all project types that are used.
    models_names = {"SiteDesign" => "Site design",
      "TechDesign" => "Tech design"}

    #key for model_name and value for name
    models_names.each do |key,value|
      p = ProjectTypes.new();
      p.model_name = key
      p.name = value
      p.save
    end

  end

  def self.down
    drop_table :project_types
  end
end

class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :projects do |t|
      t.string :type
      t.string :name
      t.text :description
      t.text :concept
      t.integer :client_id

      t.timestamps
    end
  end

  def self.down
    drop_table :projects
  end
end
  • 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-12T17:14:16+00:00Added an answer on May 12, 2026 at 5:14 pm

    Not surprising you’re getting problems. By moving from a pure STI system to your current system you are horribly breaking the patterns you are using by intermingling parts of one with parts of another.

    I’d personally go for something like:

    class Project < ActiveRecord::Base
        attr_readonly(:project_type)
        belongs_to :project_type
        before_create :set_project_type
    
        def set_project_type()
            project_type = ProjectType.find_by_model_name(this.class)
        end
    end
    
    class SiteProject < Project
    end
    
    class TechProject < Project
    end
    
    class ProjectType < ActiveRecord::Base
        has_many :projects
    end
    

    with migrations:

    class CreateProjectTypes < ActiveRecord::Migration
      def self.up
        create_table :project_types  do |t|
          t.string :model_name , :null => false
          t.string :name, :null => false
          t.text :description
    
          t.timestamps
        end
    
        add_index :project_types, :model_name, :unique => true
    
    
        #all project types that are used.
        models_names = {"SiteDesign" => "Site design",
          "TechDesign" => "Tech design"}
    
        #key for model_name and value for name
        models_names.each do |key,value|
          p = ProjectTypes.new();
          p.model_name = key
          p.name = value
          p.save
        end
    
      end
    
      def self.down
        drop_table :project_types
      end
    end
    
    class CreateProjects < ActiveRecord::Migration
      def self.up
        create_table :projects do |t|
          t.string :type
          t.references :project_type, :null => false
          t.text :description
          t.text :concept
          t.integer :client_id
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :projects
      end
    end
    

    It just cleans things up and it also helps clarify what you’re doing. Your ‘ProjectType’ table is purely for extra data, your inheritance tree still exists. I’ve also thrown in some checks to make sure your project type is always set (and correctly, based on the model name) and stops you from changing project type once it’s been saved by making the attribute read only.

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

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think there is a API function to retrieve… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer RPG won't let you do that. The best solution I… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Nobody ever answered, so I'll answer myself for posterity. :-)… May 12, 2026 at 8:36 pm

Related Questions

I am using Single-Table Inheritance (STI) on one of my models for a Rails
I am using Single Table Inheritance and have comments on all the subclasses. I
I am using hibernate as a persistence layer. There are 2 entities that live
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
I'm relatively new to NHibernate, but have been using it for the last few

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.