0

In my project, I have this following code

scenario = Scenario.new
scenarioTranslation = ScenarioTranslation.new

params[:title].each do | index |
      scenario.position = last_position + 1
      scenario.analysis_id = scenarioTranslation.scenario_id
      scenario.save
    end

Here the params[:title] is an array and I have to loop through number of array elements and save the scenario

Currently its saving only once. Can anyone tell me how to fix this error

Thanks

1
  • What exactly is it you want to do? Can you provide some context? Possibly it can be achieved in a better way. Commented Oct 19, 2012 at 9:26

2 Answers 2

3

Im a little bit confused about what your code should do but I think this is what you need:

params[:title].each_with_index do | title, index |
      scenario_translation = ScenarioTranslation.create
      scenario = Scenario.new(:position => (last_position + 1), :analysis_id => scenario_translation.id)
      scenario.save
end
Sign up to request clarification or add additional context in comments.

Comments

1

Each iteration of the loop, create a scenarioTranslation object first, then a scenario object, set the array index as position of the scenario object, and save the scenario object.

params[:title].each_with_index do | title, index |
  scenarioTranslation = ScenarioTranslation.create

  scenario = Scenario.new
  scenario.position = index
  scenario.analysis_id = scenarioTranslation.scenario_id
  scenario.save
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.