大众分配异常没有解释发现(Rails的3.2.1) [英] Mass-assign exception no explanation found (Rails 3.2.1)
问题描述
请参阅这个帖子为好。
注意:当前rake任务设置后对象时节省了用户和主题对象,而不是职位或标签
问:什么是正确的方法来描述我的rake任务这种关系(如下图所示),这样就不会引发不能大规模分配属性例外?
下面是我的步骤:
第1步:创建模型的
类岗位<的ActiveRecord :: Base的
belongs_to的:话题,inverse_of:帖子
has_and_belongs_to_many:标签
attr_accessible:标题,说明,:内容:tags_attributes
accepts_nested_attributes_for:标签,allow_destroy:真,reject_if:拉姆达{| ATTRS | attrs.all? {|键,值| value.blank?}}
结束
类标签<的ActiveRecord :: Base的
has_and_belongs_to_many:帖子
attr_accessible:TAG_NAME #UPDATE
结束
类主题<的ActiveRecord :: Base的
的has_many:帖子,inverse_of::主题
attr_accessible:TOPIC_NAME,:posts_attributes
accepts_nested_attributes_for:帖子,allow_destroy:真,reject_if:拉姆达{| ATTRS | attrs.all? {|键,值| value.blank?}}
结束
所以,我的发布
模式主题
和邮政的孩子与<$ C $一个HABTM关系C>标签。
第2步:创建一个rake任务的
命名空间:DB做
说明填充数据库的样本数据
任务:帖子=&GT; :环境做
耙::任务['DB:重置']调用。
make_users
make_topics
make_posts
结束
结束
高清make_users
却将让用户+...
5.times做| N |
NAME =法克尔:: Name.name
密码=富
电子邮件=example-#{n+1}@example.com
用户= User.create!(
codeNAME:姓名,
电子邮件:电子邮件,
密码:密码,
password_confirmation:密码)
结束
结束
高清make_topics
提出制造话题+...
3.times做
T =法克尔:: Lorem.words(1)
主题= Topic.create(TOPIC_NAME:T)!
结束
结束
高清make_posts
把决策职位+...
User.all(上限:3)。每个做|用户|
Topic.all.each做|专题|
10.times做
内容=法克尔:: Lorem.paragraphs(3)
说明=法克尔:: Lorem.words(10)
标题=法克尔:: Lorem.words(4)
tag_1 =法克尔:: Lorem.words(1)
tag_2 =法克尔:: Lorem.words(1)
tag_3 =法克尔:: Lorem.words(1)
交= user.posts.create(标题:标题,
posts_attributes:[{TOPIC_NAME:@topic}],
说明:说明,
内容:内容,
tags_attributes:[{TAG_NAME:tag_1},{TAG_NAME:tag_2},{TAG_NAME:tag_3}])
结束
结束
结束
结束
第3步:现在运行rake任务的
...
让用户...
决策职位...
耙中止!
不能大规模指派保护的属性:posts_attributes
...
...
/lib/tasks/make_posts.rake:45:in`块(3级)的make_posts
/lib/tasks/make_posts.rake:38:in'次'
/lib/tasks/make_posts.rake:38:in`块(2级)的make_posts
/lib/tasks/make_posts.rake:36:in`每个
/lib/tasks/make_posts.rake:36:in`块make_posts
/lib/tasks/make_posts.rake:35:in`每个
/lib/tasks/make_posts.rake:35:in`make_posts
/lib/tasks/make_posts.rake:7:in`块(2级)与&lt;顶(必填)&GT;
...
...
现在,因为我不能让我的rake任务去,我去到控制台,这里是错误:
主题负载(1.5毫秒)选择主题。* FROM主题LIMIT 1
=&GT; #&LT;主题ID:1,TOPIC_NAME:--- \正引渡\ N,created_at:2012-02-14 21点06分41秒,的updated_at:2012-02-14 21点06分41秒&GT;
IRB(主):007:0&GT; topic.posts_attributes = {1=&GT; {TAG_NAME:锤时间}}
加载ActiveModel :: MassAssignmentSecurity ::错误:无法大规模指派保护的属性:TAG_NAME
从/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
从/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
从/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'
从/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
从/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
从/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 `初始化
从/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 '新'
从/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
从/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
从/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 '构建'
从/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 `块assign_nested_attributes_for_collection_association
从/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 `每个
从/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
从/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 ='
从(IRB):7
从/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 '开始'
从/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 '开始'
从/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 `&LT;顶(必填)&GT;
从脚本/导轨:6:在'要求'
从脚本/导轨:6:`&lt;主&GT;IRB(主):008:0&GT;
为什么要通过 posts_attributes
到 user.posts.create
?在我看来像你想创建一个主题的,有很多帖子,在这种情况下,你应该做的 user.topics.create
代替。
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>
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.
这篇关于大众分配异常没有解释发现(Rails的3.2.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!