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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:13:26+00:00 2026-06-05T05:13:26+00:00

I have set up a HABTM relationship between two table creating a many to

  • 0

I have set up a HABTM relationship between two table creating a many to many relationship between items and categories. I want to add an item connected with one or more categories via the add item form. when I submit the form I am getting the error “Can’t mass-assign protected attributes: categories”.

Here are my models:

class Item < ActiveRecord::Base
  attr_accessible :description, :image, :name

  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  belongs_to :user
  has_and_belongs_to_many :categories

  validates :name, presence: true, length: {maximum: 50}
  accepts_nested_attributes_for :categories
end

class Category < ActiveRecord::Base
  attr_accessible :description, :name
  has_and_belongs_to_many :items

  validates :name, presence: true, length: {maximum: 50}
end

And my migrations:

class CreateItems < ActiveRecord::Migration
  def change
    create_table :items do |t|
      t.string :name
      t.text :description
      t.has_attached_file :image

      t.timestamps
    end
  end
end

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name
      t.string :description

      t.timestamps
    end
  end
end

class CreateCategoriesItems < ActiveRecord::Migration
  def up
    create_table :categories_items, :id => false do |t|
      t.integer :category_id
      t.integer :item_id
    end
  end

  def down
    drop_table :categories_items
  end
end

And my form looks like this:

<%= form_for(@item, :html => { :multipart => true }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>


  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :description %>
  <%= f.text_field :description %>
  <%= f.file_field :image %>
  <%= f.collection_select(:categories, @categories,:id,:name)%>
  <%= f.submit "Add Item", :class => "btn btn-large btn-primary" %>
<% end %>

and here’s my Items Controller:

class ItemsController < ApplicationController

  def new
    @item = Item.new
    @categories = Category.all
  end

  def create
    @item = Item.new(params[:item])
    if @item.save
      #sign_in @user
      flash[:success] = "You've created an item!"
      redirect_to root_path
    else
      render 'new'
    end
  end

  def show
  end

  def index
    @items = Item.paginate(page: params[:page], per_page: 3)
  end


end

Thanks for all of your help 🙂

-Rebekah

  • 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-05T05:13:27+00:00Added an answer on June 5, 2026 at 5:13 am

    IAmNaN posted the comment above that was the missing link in my code working properly. I have since written a blog post that details the process of getting the HABTM set-up. Thanks IAmNaN!

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

Sidebar

Related Questions

I have a table called Landmarks and a table called Categories with a HABTM
I'm having trouble with my HABTM relationship in CakePHP. I have two models like
I have a need to add metadata about a HABTM relationship. I wanted to
I have Event HABTM Category with categories_events table. I want to display only Event
I have the following relationships set up: A HABTM B B belongsTo C C
I have set up a Core Data model where I have two objects, say
So Rails doesn't have support for :through associations through a habtm relationship. There are
I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many
I have set the title for navigation item on top of the screen. But
Have been having an issue sine using the HABTM relationship in models instead of

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.