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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:43:25+00:00 2026-05-25T09:43:25+00:00

Consider this simple model, where a Project has one ProjectType and, naturally many Projects

  • 0

Consider this simple model, where a Project has one ProjectType and, naturally many Projects can be of that type.

So a Project has_one :project_type (called type) and a ProjectType has_many :projects.

In my migration I put (simplified for this example)

create_table :projects do |t|
  t.string :name, :null => false
  t.integer :type
end

create_table :project_types do |t|
  t.string :name, :null => false
end

My Project class looks like this (again simplified for this example)

#!usr/bin/ruby
require 'active_record'

class Project < ActiveRecord::Base
  has_one :type, :class_name => 'ProjectType'
end

And my ProjectType looks like

#!usr/bin/ruby
require 'active_record'

class ProjectType < ActiveRecord::Base
  has_many :projects
end

I’ve written a simple Unit Test to check this works

#test creation of Projects and related objects.
def test_projects_and_etc  
  pt = ProjectType.create(:name => 'Test PT')
  project = Project.create(:name => 'Test Project', :type => pt)
  assert project.type.name == 'Test PT', "Wrong Project Type Name, expected 'Test PT' but got '#{project.type.name}'."

  # clean up
  project.destroy
  pt.destroy
end

This test throws an error at the assert, saying

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: project_types.project_id: SELECT  "project_types".* FROM "project_types" WHERE ("project_types".project_id = 1) LIMIT 1

The SQL seems to be assuming that there is a project_id field in the project_types table but that makes no sense if a ProjectType can be associated with many Projects. I suspect that my problem is something to do with my wanting to be able to refer to the ProjectType as project.type not project.project_type, but I am not sure how I’d fix this.

  • 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-25T09:43:27+00:00Added an answer on May 25, 2026 at 9:43 am

    You need to use belongs_to instead of has_one on the project model.

    You also need to add a project_type_id column on to the projects table.

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

Sidebar

Related Questions

I know this is a simple question, but I can't figure it out. Consider
Let's consider this model: A: columns: b_id: type: int notnull: false relations: b: B
This is obviously a topic that has been discussed many times, however my angle
Consider this sample model: MODEL_CHOICES = ( (uSPAM, uSpam), (uXOUP, uEggsoup), ) (snip) type
Consider this simple Model and ViewModel scenario: public class SomeModel { public virtual Company
I have a simple photo taking application that has the following design: [Consider the
Consider this simple XML document. The serialized XML shown here is the result of
Consider this simple markup: <body> <div style=border: 2px solid navy; position:absolute; width:100%; height:100%> </div>
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
This may seem a very silly question. Consider this: I have a simple Boolean

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.