【发布时间】:2012-06-13 22:12:51
【问题描述】:
这是我的两个模型:
class Article < ActiveRecord::Base
attr_accessible :content
has_many :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content
belongs_to :article
end
我正在尝试使用以下代码在 seed.rb 中播种数据库:
Article.create(
[{
content: "Hi! This is my first article!",
comments: [{content: "It sucks"}, {content: "Best article ever!"}]
}],
without_protection: true)
但是 rake db:seed 给了我以下错误信息:
rake aborted!
Comment(#28467560) expected, got Hash(#13868840)
Tasks: TOP => db:seed
(See full trace by running task with --trace)
可以像这样为数据库播种吗?
如果是,一个后续问题:我已经搜索了一些,似乎要进行这种(嵌套?)批量分配,我需要为我想要分配的属性添加“accepts_nested_attributes_for”。 (可能类似于 Comment 模型的 'accepts_nested_attributes_for :article')
有没有办法允许这个类似于'without_protection: true'?因为我只想在做种数据库的时候接受这种海量赋值。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 activerecord mass-assignment