将对象保存到数据库

时间:2014-08-13 13:44:03

标签: ruby-on-rails serialization ruby-on-rails-4 omniauth omniauth-facebook

我在rails 4应用程序上有一个ruby,我使用omniauth-facebook gem进行身份验证。我有

@omniauth = request.env["omniauth.auth"]

现在,我想将用户的教育历史记录(@omniauth[:extra][:raw_info][:education])保存在数据库中education表的Auth列中。 @omniauth[:extra][:raw_info][:education]是一个包含OmniAuth::AuthHash个对象的数组对象(即@omniauth[:extra][:raw_info][:education][0].class返回OmniAuth::AuthHash)。

Rails api说" Active Record可以使用YAML序列化文本列中的任何对象。为此,您必须通过调用类方法serialize来指定它。这使得存储数组,散列和其他不可映射的对象成为可能,而无需进行任何额外的工作。"所以我做了以下工作,似乎工作正常:

auth.education = @omniauth[:extra][:raw_info][:education]
auth.save

这有什么缺点吗?有没有更好的方法(比如使用json等)?我可以想到的一个缺点是以这种方式保存数据会使搜索和查询变得很困难,而这些搜索和查询可能是我所不需要的。

非常感谢。

0 个答案:

没有答案
相关问题