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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:24:23+00:00 2026-06-01T13:24:23+00:00

I am wondering how to build a join table articles_categories, but one where there

  • 0

I am wondering how to build a join table articles_categories, but one where there can only be one category per article. I know the typical way to do categories is to allow multiple categories for each article with has_and_belongs_to_many, but I’d like only allow one category per article. I have already set up my articles_categories join table with the has_and_belongs_to_many associations. I followed an example from Apress Beginning Rails 3, that also recommended to set the articles_categories table :id => false. Will I also have to update the table to have an id? Thanks!

So far I have updated my articles model to has_one :category and the category model to has_many :articles, but I get an error while testing in irb:

a = Article.first
a.category

The error is:

Category Load (0.7ms)  SELECT "categories".* FROM "categories" WHERE "categories"."article_id" = 1500 LIMIT 1
PG::Error: ERROR:  column categories.article_id does not exist
LINE 1: SELECT  "categories".* FROM "categories"  WHERE "categories"...

Can somebody tell me how to better set this up? Thanks!

  • 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-06-01T13:24:24+00:00Added an answer on June 1, 2026 at 1:24 pm

    If you want each article has only one category you should not use cross table. Simply use references in migration — http://guides.rubyonrails.org/migrations.html#highlighter_472212

    rails g migration ArticleHasCategory category:references
    

    Then you specify the article has category in model

    belongs_to :category
    

    When you call article.category. It will automatically look for column <relation name>_id which should point to record in <relation name>

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

Sidebar

Related Questions

i was wondering if there's a way to build a class which can accept
Wondering if I can speed up the search. I need to build a functionality
I'm wondering if there is any build-in support for the HTML5 Notification feature in
I'm wondering it there is a way to build an Objective-C applications and then
Wondering if there is any way to build and fire an event (e.g. on
I've just started using PHPUnit and am wondering if there is a build in
I am wondering how I can build the registration process like we have in
Hey folks, I am wondering whether android allows to build apps that can send
I'm wondering if I can build and test my current project which is iOS
I am wondering how to build the follwoing scenario: - I have one app

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.