If your association is :
Game has many levels and level belongs to a game.
Then, @game = Game.find_by_id(params[:id]) #whoesoever game page is it @levels = @game.present? ? @game.levels : []
I hope, i understood your problem
Question
If I save an array using @levels.each(&:save)
how do i load it back from the model Level?
That is: what is the appropriate command to repopulate the array from the saved data
@levels = Level.find_all
Does not seem to exist, I am pretty new to this as you see, i want to store and load, levels!
Here's my GamesController:
def initialize
@levels = [] unless @levels
@levels = levels
end
def add_level
levels << Level.new
levels.each(&:save)
redirect_to edit_game_path(params[:id])
end
and the model:
class Game < ActiveRecord::Base
has_many :levels
end
Solution
If your association is :
Game has many levels and level belongs to a game.
Then, @game = Game.find_by_id(params[:id]) #whoesoever game page is it @levels = @game.present? ? @game.levels : []
I hope, i understood your problem
OTHER TIPS
@levels = Level.all
Should give you an array of level objects. Declaring this instance variable in your GamesController will make it accessible to your view.