我有一个我正在开发的应用程序,它是一个视频流应用程序,我试图实现的功能是playlist(用户可以使用唯一的名称查看的视频集合)。
在我的关联中:
user => has_many playlists,
video => belongs_to playlists,
playlist => belongs_to userps。在我的应用程序中,一般用户不能创建视频,只有管理员。
创建播放列表后,用户应该能够通过按钮将视频添加到播放列表中。如果我没记错,播放列表应该是一个数组,会有一个函数来添加_to _playlist,这个函数将通过video_id检索视频,然后将其放入数组中,并返回最终的播放列表。
我的主要问题是,我很困惑如何做implementation.any帮助会很高兴。
发布于 2013-09-05 00:44:28
如果您正在使用ActiveRecord并希望将数组与模型的实例相关联,则应使用serialize类方法。
来自the docs
class User < ActiveRecord::Base
serialize :preferences
end
user = User.create(preferences: { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }该示例使用散列,但serialize也允许使用数组。如果要将该属性限制为仅数组,可以这样做:
serialize :attr, Arrayhttps://stackoverflow.com/questions/18619384
复制相似问题