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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:01:52+00:00 2026-05-29T20:01:52+00:00

Please see this post as well. Note: The current rake task saves User and

  • 0

Please see this post as well.

Note: The current rake task saves User and Topic objects, but not posts or tags when setting a post object.

Question: What is a proper way to describe this relationship in my rake task (shown below) so that it does not throw a can’t mass assign attributes exception?

Here were my steps:

Step 1: Create models

class Post < ActiveRecord::Base
  belongs_to :topic, inverse_of: :posts
  has_and_belongs_to_many :tags
  attr_accessible :title, :description, :content, :tags_attributes
  accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}}
end

class Tag < ActiveRecord::Base
 has_and_belongs_to_many :posts
 attr_accessible :tag_name #UPDATE
end

class Topic < ActiveRecord::Base
  has_many :posts, inverse_of: :topic
  attr_accessible :topic_name, :posts_attributes
  accepts_nested_attributes_for :posts, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}}
end

Therefore, my Post model is a child of Topic and Post has a HABTM relationship with Tags.

Step 2: Create a rake task

namespace :db do
desc "Fill database with sample data"
task :posts => :environment do
 Rake::Task['db:reset'].invoke
 make_users
 make_topics
 make_posts
 end
end

def make_users
 puts "making users" + "..."
 5.times do |n|
  name  = Faker::Name.name
  password = "foo"
  email = "example-#{n+1}@example.com"
    user = User.create!(
                codename: name,
                email: email,
                password: password,
                password_confirmation: password)
 end
end

def make_topics
  puts "making topics"+"..."
  3.times do
  t = Faker::Lorem.words(1)
  topic = Topic.create!(topic_name: t)
 end
end

def make_posts
 puts "making posts" + "..."
 User.all(limit: 3).each do |user|
  Topic.all.each do |topic|

    10.times do
      content = Faker::Lorem.paragraphs(3)
      description = Faker::Lorem.words(10)
      title = Faker::Lorem.words(4)
      tag_1 = Faker::Lorem.words(1)
      tag_2 = Faker::Lorem.words(1)
      tag_3 = Faker::Lorem.words(1)
      post = user.posts.create!(title: title, 
                                posts_attributes: [{topic_name: @topic}], 
                                description: description, 
                                content: content, 
                                tags_attributes: [{tag_name: tag_1}, {tag_name: tag_2}, {tag_name: tag_3}])
    end
  end
 end

end

Step 3: Now run the rake task

...
making users...
making posts...
rake aborted!
Can't mass-assign protected attributes: posts_attributes
...
...
/lib/tasks/make_posts.rake:45:in `block (3 levels) in make_posts'
/lib/tasks/make_posts.rake:38:in `times'
/lib/tasks/make_posts.rake:38:in `block (2 levels) in make_posts'
/lib/tasks/make_posts.rake:36:in `each'
/lib/tasks/make_posts.rake:36:in `block in make_posts'
/lib/tasks/make_posts.rake:35:in `each'
/lib/tasks/make_posts.rake:35:in `make_posts'
/lib/tasks/make_posts.rake:7:in `block (2 levels) in <top (required)>'

...
...

Now since I could not get my rake task going, I went into the console and here is the error:

Topic Load (1.5ms)  SELECT "topics".* FROM "topics" LIMIT 1
=> #<Topic id: 1, topic_name: "---\n- aut\n", created_at: "2012-02-14 21:06:41", updated_at: "2012-02-14 21:06:41">
irb(main):007:0> topic.posts_attributes = {"1"=>{tag_name:"hammer time"}}
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: tag_name
 from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activemodel-3.2.1/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
  from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activemodel-3.2.1/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
  from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activemodel-3.2.1/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
  from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activemodel-3.2.1/lib/active_model/mass_assignment_security.rb:228:in `sanitize_for_mass_assignment'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/base.rb:495:in `initialize'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/reflection.rb:183:in `new'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/reflection.rb:183:in `build_association'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/association.rb:233:in `build_record'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:112:in `build'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/nested_attributes.rb:405:in `block in assign_nested_attributes_for_collection_association'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/nested_attributes.rb:400:in `each'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/nested_attributes.rb:400:in `assign_nested_attributes_for_collection_association'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/nested_attributes.rb:288:in `posts_attributes='
   from (irb):7
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
   from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /home/rhodee/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'irb(main):008:0>
  • 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-29T20:01:54+00:00Added an answer on May 29, 2026 at 8:01 pm

    Why are you passing posts_attributes to user.posts.create? It looks to me like you’re wanting to create a topic that has many posts, in which case you should be doing user.topics.create instead.

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

Sidebar

Related Questions

Please forgive me if this post is in the wrong place, but as your
Please see this post for code example : How to map Type with Nhibernate
First, please see this post on Doctype I've been trying to get an answer
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
Important : Please see this very much related question: Return multiple values in C++
It seems that i cannot access $(this) inside jquery ajax success function. please see
Please see the form HTML code below <form method=post action=register> <div class=email> Email <input
Please point me to any dupes or better places to post this question that
Someone please help me return this list properly from my view. I don't see
Well I was reading this post and then I came across a code which

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.