ruby-on-rails - 批量分配异常未找到解释(Rails 3.2.1)

标签 ruby-on-rails activerecord activemodel

请参阅this post

注意:当前的 rake 任务在设置帖子对象时保存 User 和 Topic 对象,但不保存帖子或标签。

问题:在我的 rake 任务(如下所示)中描述这种关系的正确方法是什么,这样它就不会抛出无法批量分配属性异常?

这是我的步骤:

第 1 步:创建模型

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

因此,我的 Post 模型是 Topic 的子模型,并且 Post 与 Tags 具有 HABTM 关系。

第 2 步:创建 rake 任务

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

结束

第 3 步:现在运行 rake 任务

...
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)>'

...
...

现在由于我无法执行我的 rake 任务,我进入了控制台,这是错误:

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>

最佳答案

为什么要将 posts_attributes 传递给 user.posts.create?在我看来,您想要创建一个包含许多帖子的主题,在这种情况下,您应该改为执行 user.topics.create

关于ruby-on-rails - 批量分配异常未找到解释(Rails 3.2.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9269142/

相关文章:

mysql - 事件模型在 new 和 create 方法调用中插入 NULL 值

javascript - 我可以获得访问我网站的本地公网 IP 地址吗?

ruby-on-rails - 使用 RSpec 3 在 block 之前运行一次

ruby-on-rails - 分区数据库表的 Rails 迁移索引名称太长

ruby-on-rails - 获取与模型关联的嵌套属性

php - 从 CodeIgniter 中的不同表中删除行

javascript - 在 Rails 中找不到 Bootstrap

mysql - 按字段排序

ruby-on-rails - Rails ActiveModel 序列化程序 : Retrieving Deeply Nested ActiveRecord Association

ruby-on-rails - rails : Getting list of attributes with uniqueness validations from a model