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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:59:19+00:00 2026-06-16T13:59:19+00:00

If I have two models: Experience and Category which are a many to many

  • 0

If I have two models: Experience and Category which are a many to many association, that looks like this:

class Experience < ActiveRecord::Base
  has_and_belongs_to_many :categories
end


class Category < ActiveRecord::Base
  has_and_belongs_to_many :experiences
end

Why sometimes people add a:

attr_accessible :category_ids in the Experience model? I have found myself having to do that so in Rails admin gem I can add categories to a particular experience, but I fail to see why.

  • 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-16T13:59:20+00:00Added an answer on June 16, 2026 at 1:59 pm

    If you setup a HABTM relation between Experience and Category, you have, besides other things, available method Experience#category_ids. This method returns individual ids of categories the current experience has.

    Experience.first.category_ids
    

    Now this method has also Experience#category_ids= variant, so you can use it to assign ids of categories:

    Experience.first.category_ids = [1, 2, 3]
    

    Now when you have a form that let’s you select categories for an experience, you have only the ids of the selected categories. When you submit the form, those ids get passed to Experience#category_ids= via mass assigment and if you don’t have this category_ids in attr_accessible, you get an error.

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

Sidebar

Related Questions

I have two related models such as this: class PartCategory < ActiveRecord::Base has_many :part_types
I have two models User and Category that have a HABTM association. I would
I have two models, Landscape: class Landscape < ActiveRecord::Base has_many :images, :as => :imageable
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two Models, Programme and Event, a programme has many events. I need
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models, A and B, and one light, L. I would like
As the title suggests I have two models Products And Orders which are actually
I've two models in my app that are joined together using a many -to-many
I have two functions that loop through all models in a Backbone collection, 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.