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

The Archive Base Latest Questions

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

I have the following data: A post called Hello has categories greet Another post

  • 0

I have the following data:

  1. A post called Hello has categories greet
  2. Another post called Hola has categories greet, international

My schema is:

create_table "posts", :force => true do |t|
  t.string "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "categories", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "posts_categories", :force => true do |t|
  t.integer  "post_id"
  t.integer  "category_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

After reading the Rails guide, the most suitable relationship for the above seems to be:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

class Category < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

My junction table also seems to have a primary key. I think I need to get rid of it.

  1. What’s the initial migration command to generate a junction table in Rails?
  2. What’s the best course of action, should I drop posts_categories and re-create it or just drop the primary key column?
  3. Does the junction table have a corresponding model? I have used scaffold to generate the junction table code, should I get rid of the extra code?

Assuming all the above has been fixed and is working properly, how do I query all posts and display them along with their named categories in the view. For example:

Post #1 - hello, categories: greet
Post #2 - hola, categories: greet, international
  • 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-14T00:16:27+00:00Added an answer on May 14, 2026 at 12:16 am

    you might want to check out this web page from the rails API doc.

    1. the easiest way to generate a junction table is "script/generate model categories_posts category_id:integer post_id:integer". Note that the class names should be in alphabetical order. I’m fairly indifferent to the whole primary key thing, but if it becomes an issue, you can generate a migration to drop like ‘script/generate DropPostsCategoriesIdFromPostsCategories posts_categories_id:integer’ (make sure this migration file does what you want, i haven’t tested it and what it does may vary by your version of rails) and then do rake db:migrate. to change the DB.

    2. for the class name you’re using, you could use:

    class Post < ActiveRecord::Base
       has_many :categories, :through => :posts_categories
    end

    has_many through lets you specify the names of the join/junction table. or you could drop the table and regenerate it with the right name.

    1. (should be #3) yeah, just generate a model for a join class, don’t do the entire scaffold. (see above)

    to find all posts, do something like @posts = Post.find(:all)

    to print out the categories, do something like

    @posts.each do | post |
       print post.name, "\n"
       @posts.categories.each do | cat |
            print "\t", cat.name, "\n"
       end
    end
    

    in actual rails code, you’d want to do that in the view, this is more of a console output-type thing.

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

Sidebar

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.