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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:41:46+00:00 2026-05-16T22:41:46+00:00

I created a joined table for the models category and products (both created with

  • 0

I created a joined table for the models category and products (both created with scaffold). The Product model is this:

class Product < ActiveRecord::Base
  belongs_to :category 

  def category_id
    category.id if category
  end

  def category_id=(id)
    self.category = Category.find_by_id(id) unless id.blank?
  end
end

and the category model is this:

class Category < ActiveRecord::Base
  has_and_belongs_to_many :products
end

In the form.html.erb I create a dropbox with all the classes for the user to choose from:

<p>
      <label for="product_category_id">Category:</label><br />
      <%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => "Select a Category" %>
</p>

Yet when I take a look at the show of the product:

<p>
  <b>Category:</b>
  <%= @product.category_id %>
</p>

or the list of the products (index.html.erb):

<td><%= product.category_id %></td>

There’s no category. Just blank. I don’t get it. Is something wrong with the category_id method or the association?

  • 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-16T22:41:47+00:00Added an answer on May 16, 2026 at 10:41 pm

    Firstly, you don’t need the explicit category_id and category_id= methods. ActiveRecord will handle those for you for a belongs_to association.

    Secondly, there seems to be a mismatch between whether you want a has_and_belongs_to_many or a has_many/belongs_to association. If you have a join table then you have the former, in which case both sides of the association should be declared with has_and_belongs_to_many. If you are just using a category_id on the products table then the other end of your association on Category should be has_many :products.

    With a join model:

    class Categorization < ActiveRecord::Base
      belongs_to :category
      belongs_to :product
    end
    

    you would define in your Product class:

    has_many :categorizations
    has_many :categories, :through => :categorizations
    

    Then, because your association is a ‘many’ association, you do not get a .category method on a product. You do however get a categories method (plus several more methods – look at the has_many documentation). If you name your collection_select category_ids then it should work as expected. You may also want to add the ‘multiple’ option to the select in order to choose more than one category.

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

Sidebar

Related Questions

I have joined models like this: // /foo/lib/model/doctrine/PurchasedItemTable.class.php public function retrievePurchased(Doctrine_Query $q) { $rootAlias
I have the following models: class Match < ActiveRecord::Base has_and_belongs_to_many :teams end And class
I am using the concrete table inheritance with SQLAlchemy. In declartive style model class,
I have a User model and Interest Model being joined by a join table
Suppose you have two models, User and City, joined by a third model CityPermission:
I created an indexed view that joined a number of tables to get better
I created a following class within a namespace Global namespace Global { public static
I created a new product type and everything works except the indexing of the
So let's say I have two :has_and_belongs_to_many models that are joined by a simple
I created a report that pulled 1 object from the Purchase_order table (PO_REF) and

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.