[英]Load model to array in rails
If I save an array using @levels.each(&:save)
how do i load it back from the model Level? 如果我使用
@levels.each(&:save)
保存数组,如何从模型级别加载它? 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: 这是我的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
@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.
在GamesController中声明此实例变量将使您的视图可以访问它。
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 = Game.find_by_id(params [:id])#whoeoever游戏页面是@levels = @ game.present? ?
? @game.levels : []
@ game.levels:[]
I hope, i understood your problem 我希望,我理解你的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.