Seed has_many relation using mass assignment
- by rnd
Here are my two models:
class Article < ActiveRecord::Base
attr_accessible :content
has_many :comments
end
class Comment < ActiveRecord::Base
attr_accessible :content
belongs_to :article
end
And I'm trying to seed the database in seed.rb using this code:
Article.create(
[{
content: "Hi! This is my first article!",
comments: [{content: "It sucks"}, {content: "Best article ever!"}]
}],
without_protection: true)
However rake db:seed gives me the following error message:
rake aborted!
Comment(#28467560) expected, got Hash(#13868840)
Tasks: TOP => db:seed
(See full trace by running task with --trace)
It is possible to seed the database like this?
If yes a follow-up question: I've searched some and it seems that to do this kind of (nested?) mass assignment I need to add 'accepts_nested_attributes_for' for the attributes I want to assign. (Possibly something like 'accepts_nested_attributes_for :article' for the Comment model)
Is there a way to allow this similar to the 'without_protection: true'? Because I only want to accept this kind of mass assignment when seeding the database.