问题描述
我开发一个电子学习Rails中,我想了一组阵列保存到数据库中,通过电子学习的各个部分跟踪用户的进步的目的。
I'm developing an e-learning in Rails and I want to save a set of Arrays to the database, with the aim of tracking a user's progress through the various sections of the e-learning.
我已经遇到这个问题,答案是:结果
<一href=\"http://stackoverflow.com/questions/21312278/storing-arrays-in-database-json-vs-serialized-array/21315340?noredirect=1#comment40564161_21315340\">Storing阵列数据库:JSON序列化与阵列结果
......这听起来像它可能是有用的,但我无法弄清楚如何将其集成到Rails项目我发展。
I've come across this question and answer:
Storing arrays in database : JSON vs. serialized array
...which sounds like it might be useful, but I can't figure out how to integrate it into the Rails project I'm developing.
由于我是pretty太大的Rails的小白,可能有人向我解释,用简单的英语(或者普通code)我会怎样:结果
A)保存到数据库中持有假的布尔序列,当用户最初签约的数组。结果
B)检索和更新来自整个电子学习各个页面的阵列。
Given that I'm pretty much a Rails noob, could someone explain to me, in plain English (or alternatively plain code) how I would:
a) save to a database an Array which holds a sequence of 'false' Booleans when a user initially signs up.
b) retrieve and update that Array from various pages throughout the e-learning.
我想说明什么我都试过了,但它只是一直在黑暗中刺野外,我真的不知道从哪里开始。如果评论类保存在控制器文件夹?或者是,在我的例子,其实User类(其中,同样,扩展ActiveRecord的)?
I would set out what I have tried, but it's just been wild stabs in the dark and I don't really know where to start. Should the Comment class be saved in the controllers folder? Or is it, in my example, actually the User class (which, similarly, extends ActiveRecord)?
再次,任何帮助(最好解释其中code属于)多AP preciated。
Once again, any help (ideally explaining where the code belongs) much appreciated.
更新
我一直在问更具体,所以我会尝试:结果
我在数据库中,这(predictably)有几个领域,包括电子邮件,用户名等。我想5个字段添加到用户表用户表,每个存储阵列来跟踪用户的进度通过各5节。我设想,当用户打开一个内容片段,所述阵列将被更新,并相应索引将被更新为真来标记,该部分的那部分就完成了。
I've been asked to be more specific, so I will try:
I have a user table in the database, which (predictably) has several fields, including email, username, etc. I'd like to add 5 more fields to the user table, each storing an Array to track the user's progress through each of the 5 sections. I'm envisaging, when a user opens a piece of content, the Array would be updated and the appropriate index would be updated to 'true' to flag that THAT part of the section is complete.
我希望帮助...
重新更新
当用户第一次签约我想要的阵列与假的布尔序列进行设置。从该文件中我将设置阵列?的是,在用户做了什么?目前,我想这一点,但我不认为这些值被设置 - 虽然它不是抛出一个错误。
When a user first signs up I want the Arrays to be set with a sequence of 'false' Booleans. From which file would I set the Arrays? Is that done in User? At the moment I'm trying this, but I don't think the values are being set - although it's not throwing an error.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
serialize :leadership_styles_progress
serialize :skills_progress
serialize :safeguarding_progress
serialize :tools_progress
before_create :set_progress_vars
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :age, presence: { :message => ": Please choose your age" }
validates :section, presence: { :message => ": Please choose the section you belong to"}
has_many :posts
private
def set_progress_vars
self.leadership_styles_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.skills_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.safeguarding_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
self.tools_progress = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
end
end
我还检查该路由的地方,通过映射'耙路线';用户/ sign_up被映射到设计/注册/新。如此做连载code,以下建议,去那里?
I have also checked which routes are mapped where, via 'rake routes'; users/sign_up is mapped to devise/registrations/new. So does the serialize code, suggested below, go there?
推荐答案
您提到的评论和用户类扩展ActiveRecord的。这些模型和他们在app / models文件夹走了。
You mentioned the Comment and User class extend ActiveRecord. These are models and they go in the app/models folder.
数组保存到数据库中是非常简单的为您链接到答案解释。所有你需要做的将它集成到你的项目申报要阵列存储,他们会在保存或更新可以自动的序列化和反序列化时,你想读他们回哪个属性。
Saving arrays to the database is very straightforward as explained in the answer you linked to. All you have to do to integrate it into your project is declare which attribute you want to store Arrays in and they will be automagically serialized upon saving or updating and deserialized when you want to read them back.
您一定要张贴任何code你已经。我假设你要序列是在注释模型的属性,它被称为选择
(当然也可以是任何东西),它是一个文本字段(不是字符串字段,因为这可能太短,如果你的阵列成长系列化当超过255个字符):
You should definitely post any code you have already. I'll assume the attribute you want to serialize is in the Comment model and it's called choices
(but of course it can be anything) and it is a text field (not a string field as this might be too short if your arrays grow more than 255 characters when serialized):
# app/models/comment.rb
class Comment < ActiveRecord::Base
serialize :choices
end
这就是它!现在,当你永远值分配给该属性 @ comment.choices = [假的,假的]
将自动当您保存序列化为YAML,然后它会被反序列化当你读它:
That's it! Now when ever you assign a value to that attribute @comment.choices = [false, false]
it will be automatically serialized as YAML when you save and then it'll be deserialized when you read it:
class CommentController < ApplicationController
def create
@comment = Comment.new choices: [false, false, true, 'stuff', 'whatever']
end
def show
@comment = Comment.find params[:id]
@comment.choices # returns [false, false, true, 'stuff', 'whatever']
end
end
要解释这是怎么回事引擎盖下当你保存记录与分配给选择
字段的ActiveRecord知道要序列化它,因为你一个数组值声明连载:选择
在您的评论类。这将需要的数组值和序列化是这样的:
To explain what's going on "under the hood" when you save the record with an array value assigned to the choices
field ActiveRecord knows that you want to serialize it since you declared serialize :choices
in your Comment class. It will take that array value and serialize it like this:
YAML.dump(choices)
这将数组转换成字符串:
this will convert the array into this string:
"---\n- false\n- false\n- true\n- stuff\n- whatever\n"
这是重新数组的presentation(序列化)。要阅读它放回(当你调用 comment.choices
)的ActiveRecord将序列化的字符串和反序列化:
That is the YAML representation (serialization) of the array. To read it back in (when you call comment.choices
) ActiveRecord will take the serialized string and deserialize it:
YAML.load(choices)
这会再转换YAML字符串回一个Ruby数组。
That will then convert the YAML string back into a Ruby array.
要总结一下:
- 添加文本字段到模型如
选择:文字
当您创建迁移 - 声明
连载:选择
在模型 - 使用正常。
- Add a text field to your model e.g.
choices:text
when you create the migration. - Declare
serialize :choices
in your model. - Use as normal.
希望有所帮助。
这篇关于保存和检索Rails中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!