In a nutshell
I run into the following error with validations / model saving
NameError (uninitialized constant PolymorphicAssociation):
Background & code
Consider the following models (omitting 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'
I am trying to find or update existing MediaViews
through a service
# 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!
Upon saving the @view I run into
NameError (uninitialized constant Media):