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

  • Home
  • SEARCH
  • 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 8537627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:56:46+00:00 2026-06-11T10:56:46+00:00

The problem is that I get this error: ActiveModel::MassAssignmentSecurity::Error: Can’t mass-assign protected attributes: amenity_id

  • 0

The problem is that I get this error:

ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: amenity_id

when I run this code:

task import_amenities: :environment do

  agent = Mechanize.new

  Kindergarten.find_all_by_public(false).each do |k| 
    p = agent.get(k.uri)
    amenities = p.search("td td tr:nth-child(11) td:nth-child(2)").text.split(/(;|,) */)
    amenities.each do |a|
      am = Amenity.find_or_create_by_name!("#{a}")
      k.update_attributes(amenity_id: am.id)
    end
  end
end

Kindergartens and Amenities are linked through a HABTM relation and are are defined as below:

kindergarten.rb

class Kindergarten < ActiveRecord::Base
  attr_accessible :location, :name, :public, :uri, :address, :contact, 
                  :phone, :url, :email, :description, 
                  :password, :password_confirmation, :amenity_ids
  has_and_belongs_to_many :amenities
end

amenity.rb

class Amenity < ActiveRecord::Base
  attr_accessible :name, :kindergarten_ids
  has_and_belongs_to_many :kindergartens
end

and here’s the migration for the join table:

class CreateKindergartensAmenitiesJoinTable < ActiveRecord::Migration
  def up

    create_table :kindergartens_amenities, :id => false do |t|
      t.integer :kindergarten_id
      t.integer :amenity_id
    end
  end
end

The error is caused by this line in the rake task:

k.update_attributes(amenity_id: am.id)

Everything seems to work great in the console until I reach the mass assignment. And I think i am really messing something up here since I’ve never used before HABTM.

Any thoughts?

  • 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-11T10:56:47+00:00Added an answer on June 11, 2026 at 10:56 am

    I couldn’t sleep last night because of this bug but I finally found the solution.

    there are a few issues in the code and the first one i noticed once i started digging and adding data in the db manually is that the join table is wrongfully named. Fix for that:

    class RenameKindergartensAmenitiesTable < ActiveRecord::Migration
      def up
        rename_table :kindergartens_amenities, :amenities_kindergartens
      end
    end
    

    apparently the habtm association is has to have stuff put alphabetically in title. source

    Second problem is that I assumed that

    k.amenity_id = am.id
    

    would add an amenity_id / kindergarten_id for each amenity existing. In fact k.amenity_id does not mean anything (especially in the case of many ids). The solution that worked is this:

    amenities.each do |a|
      am = Amenity.find_or_create_by_name!("#{a}")
      k.update_attributes(amenity_ids: k.amenity_ids.push(am.id))
    end
    

    I haven’t modified the attr_accessible anywhere

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

Sidebar

Related Questions

My problem is that i get this error binarytree.cpp: In member function ‘void BinaryTree<T>::printPaths(const
I'm stuck on an aggregation problem that I can't get to the bottom of.
I'm running into a problem that I can't seem to get past. Whenever I
when starting my Repast Simphony Model in Batch run I get this error message:
I get this error when I run rake db:migrate *db/migrate//004_add_data_to_measurement_type_and_measurement_unit.rb:3: invalid multibyte char (US-ASCII)
i know, this is a realy popular error. But my problem is that my
Can you please help me with this problem that I am facing last days.
I'm having a problem that a statusbar get displayed over the content of the
I have a problem that I'm hoping to get some direction in solving -
I have a small problem that I am hoping to get some help with.

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.