Reputation: 59
I have model Exhibit and he have quiz. In quiz we have questions and answers belongs to it. I used nested attributes.
Where I make mistake, because wright now after rake db:seed rails add me only last line in Question. Here is example one of my seeds exhibit
e1 = Exhibit.create(
title: 'TITLE',
author: 'PICASOO',
date_of_origin: '300',
description: 'LOREM IPSUM',
ex_id: '1',
type: nil,
questions_attributes:[
content: "QUESTION 1?",
answers_attributes:[
content: "ANSWER 1",
correct: true,
content: "ANSWER 2",
correct: false, # if it's correct answer i change this var.
content: "ANSWER 3",
correct: false]
])
Exhibit model:
has_many :questions, :dependent=> :destroy
accepts_nested_attributes_for :questions,
:reject_if => lambda { |a| a[:content].blank? },
:allow_destroy => true
Question model:
belongs_to :exhibit
has_many :answers, :dependent=> :destroy
accepts_nested_attributes_for :answers,
:reject_if => lambda { |a| a[:content].blank? },
:allow_destroy => true
Answer model:
class Answer < ActiveRecord::Base
belongs_to :question
end
Upvotes: 1
Views: 2805
Reputation: 5905
e1 = Exhibit.create(
title: 'TITLE',
author: 'PICASOO',
date_of_origin: '300',
description: 'LOREM IPSUM',
ex_id: '1',
type: nil,
{
:questions_attributes =>
{ 0 =>
{
content: "Q1",
:answers_attributes =>
{ 0 =>
{
content: "ANSWER 1",
correct: true
},
1 =>
{
content: "ANSWER 2",
correct: false
}
}
}
}
}
)
or:
e1 = Exhibit.create(
title: 'TITLE',
author: 'PICASOO',
date_of_origin: '300',
description: 'LOREM IPSUM',
ex_id: '1',
type: nil,
questions_attributes: [
{
content: 'Q1',
answers_attributes: [
{content: "A1", correct: false},
{content: "A2", correct: true}
]
}
]
)
Upvotes: 8