0
忽略簡而言之多態性通過mongoid
我碰上節約以下錯誤與驗證/模型
NameError (uninitialized constant PolymorphicAssociation):
背景&代碼
考慮以下車型(省略Mongoid::Document
)
class User
has_many :media_views
class MediaView
field :last_seen_at, type: DateTime
belongs_to :user
belongs_to :media, polymorphic: true
class Image
has_many :views, inverse_of :media, class_name: 'MediaView'
class Video
has_many :views, inverse_of :media, class_name: 'MediaView'
我試圖找到或更新現有MediaViews
通過服務
# my_view_service.rb
class ViewService
def initialize(user, media)
@user = user
@media = media
end
def just_viewed!
set_view
@view.last_seen_at = Time.now
@view.save
end
def set_view
@view = MediaView.where(
user: @user,
media: @media,
).first_or_initialize
end
ViewService.new(User.first, Image.first).just_viewed!
在保存我運行@view到
NameError (uninitialized constant Media):