Okay, well here's the real code I'm dealing with (sorry if its long).
I'm adding surveys to a research project I've been working on, So I've
got three migration files, one to create the surveys, one to create
the survey questions, and another to create the possible survey
answers.
###_create_surveys.rb:
class CreateSurveys < ActiveRecord::Migration
def self.up
create_table :surveys do |t|
t.boolean :required
t.string :title
t.datetime :go_live
t.timestamps
end
end
def self.down
drop_table :surveys
end
end
###_create_survey_answers.rb:
class CreateSurveyQuestions < ActiveRecord::Migration
def self.up
create_table :survey_questions do |t|
t.integer :position
t.text :question
t.integer :survey_id
t.string :answer_style
t.timestamps
end
end
def self.down
drop_table :survey_questions
end
end
###_create_survey_possible_answers:
class CreateSurveyPossibleAnswers < ActiveRecord::Migration
def self.up
create_table :survey_possible_answers do |t|
t.integer :position
t.text :answer
t.integer :survey_question_id
t.timestamps
end
end
def self.down
drop_table :survey_possible_answers
end
end
And I've got 3 models to go with the new classes.
survey.rb:
class Survey < ActiveRecord::Base
has_many :survey_questions, :order => :position
end
survey_questions.rb:
class SurveyQuestions < ActiveRecord::Base
belongs_to :survey
acts_as_list :scope => :survey
has_many :survey_possible_answers
end
survey_possible_answers.rb:
class SurveyPossibleAnswers < ActiveRecord::Base
belongs_to :survey_questions, :order => :position
acts_as_list :scope => :survey_questions
end
And finally, here's the test data I was trying to use.
surveys.yml:
initial_survey:
required: true
title: Initial Survey
go_live: 2009-01-01 00:00:00
past_weekly_1:
required: false
title: Past Weekly One
go_live: 2009-02-01 00:00:00
past_weekly_2:
required: false
title: Past Weekly Two
go_live: 2009-02-01 00:00:00
future_weekly_1:
required: false
title: Future Weekly One
go_live: <%= Date.today.next.strftime( "%Y-%m-%d" ) %>
future_requred_1:
required: true
title: Future Required
go_live: <%= Date.today.next.strftime( "%Y-%m-%d" ) %>
survey_questions.yml:
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
<%
surveys = [ "initial_survey", "past_weekly_1", "past_weekly_2",
"future_weekly_1", "future_requred_1" ]
%>
<% surveys.each do |survey| %>
<%= survey %>_q1:
position: 1
question: My Question?
survey: <%= survey %>
<%= survey %>_q2:
position: 2
question: My Question?
survey: <%= survey %>
<%= survey %>_q3:
position: 3
question: My Question?
survey: <%= survey %>
<% end %>
survey_possible_answers.yml:
<%
surveys = [ "initial_survey", "past_weekly_1", "past_weekly_2",
"future_weekly_1", "future_requred_1" ]
numQuestions = 3
%>
<% surveys.each do |survey| %>
<% for i in (1..numQuestions) %>
<%= survey %>_q<%= i %>_a1:
position: 1
answer: Some Answer
survey_question: <%= survey %>_q<%= i %>
<%= survey %>_q<%= i %>_a2:
position: 2
answer: Some Answer
survey_question: <%= survey %>_q<%= i %>
<%= survey %>_q<%= i %>_a3:
position: 3
answer: Some Answer
survey_question: <%= survey %>_q<%= i %>
<% end %>
<% end %>
So .... after all that, I get errors like this when I try to run rake
test:units:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column
'survey_question' in 'field list': INSERT INTO
`survey_possible_answers` (`answer`, `survey_question`, `position`)
VALUES ('Some Answer', 'future_requred_1_q1', 1)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---