Guest User

Untitled

a guest
Aug 3rd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. Mass-assign exception no explanation found (Rails 3.2.1)
  2. class Post < ActiveRecord::Base
  3. belongs_to :topic, inverse_of: :posts
  4. has_and_belongs_to_many :tags
  5. attr_accessible :title, :description, :content, :tags_attributes
  6. accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}}
  7. end
  8.  
  9. class Tag < ActiveRecord::Base
  10. has_and_belongs_to_many :posts
  11. attr_accessible :tag_name #UPDATE
  12. end
  13.  
  14. class Topic < ActiveRecord::Base
  15. has_many :posts, inverse_of: :topic
  16. attr_accessible :topic_name, :posts_attributes
  17. accepts_nested_attributes_for :posts, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}}
  18. end
  19.  
  20. namespace :db do
  21. desc "Fill database with sample data"
  22. task :posts => :environment do
  23. Rake::Task['db:reset'].invoke
  24. make_users
  25. make_topics
  26. make_posts
  27. end
  28. end
  29.  
  30. def make_users
  31. puts "making users" + "..."
  32. 5.times do |n|
  33. name = Faker::Name.name
  34. password = "foo"
  35. email = "example-#{n+1}@example.com"
  36. user = User.create!(
  37. codename: name,
  38. email: email,
  39. password: password,
  40. password_confirmation: password)
  41. end
  42. end
  43.  
  44. def make_topics
  45. puts "making topics"+"..."
  46. 3.times do
  47. t = Faker::Lorem.words(1)
  48. topic = Topic.create!(topic_name: t)
  49. end
  50. end
  51.  
  52. def make_posts
  53. puts "making posts" + "..."
  54. User.all(limit: 3).each do |user|
  55. Topic.all.each do |topic|
  56.  
  57. 10.times do
  58. content = Faker::Lorem.paragraphs(3)
  59. description = Faker::Lorem.words(10)
  60. title = Faker::Lorem.words(4)
  61. tag_1 = Faker::Lorem.words(1)
  62. tag_2 = Faker::Lorem.words(1)
  63. tag_3 = Faker::Lorem.words(1)
  64. post = user.posts.create!(title: title,
  65. posts_attributes: [{topic_name: @topic}],
  66. description: description,
  67. content: content,
  68. tags_attributes: [{tag_name: tag_1}, {tag_name: tag_2}, {tag_name: tag_3}])
  69. end
  70. end
  71. end
  72.  
  73. ...
  74. making users...
  75. making posts...
  76. rake aborted!
  77. Can't mass-assign protected attributes: posts_attributes
  78. ...
  79. ...
  80. /lib/tasks/make_posts.rake:45:in `block (3 levels) in make_posts'
  81. /lib/tasks/make_posts.rake:38:in `times'
  82. /lib/tasks/make_posts.rake:38:in `block (2 levels) in make_posts'
  83. /lib/tasks/make_posts.rake:36:in `each'
  84. /lib/tasks/make_posts.rake:36:in `block in make_posts'
  85. /lib/tasks/make_posts.rake:35:in `each'
  86. /lib/tasks/make_posts.rake:35:in `make_posts'
  87. /lib/tasks/make_posts.rake:7:in `block (2 levels) in <top (required)>'
  88.  
  89. ...
  90. ...
  91.  
  92. Topic Load (1.5ms) SELECT "topics".* FROM "topics" LIMIT 1
  93. => #<Topic id: 1, topic_name: "---n- autn", created_at: "2012-02-14 21:06:41", updated_at: "2012-02-14 21:06:41">
  94. irb(main):007:0> topic.posts_attributes = {"1"=>{tag_name:"hammer time"}}
  95. ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: tag_name
  96. 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'
  97. 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'
  98. 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'
  99. 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'
  100. 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'
  101. 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'
  102. 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'
  103. 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'
  104. 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'
  105. 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'
  106. 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'
  107. 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'
  108. 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'
  109. 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='
  110. from (irb):7
  111. 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'
  112. 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'
  113. 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)>'
  114. from script/rails:6:in `require'
  115. from script/rails:6:in `<main>'irb(main):008:0>
Add Comment
Please, Sign In to add comment