0

i working on a little app to show some pictures. Each picture can be "voted" or "qualified" in a scale of 1 to 5.

The qualification can be done for a logged user only one time.

I need to know the qualification of each image, and what image a user set the vote (and know the value of that vote), so i create this models:

class Voto
    include Mongoid::Document
    embedded_in :picture
    embedded_in :user
    field :value, :type => Integer
end

class Picture
    include Mongoid::Document
    embeds_many :votos
    embeds_many :comments
    belongs_to  :user
    ...
    ...
end

class User
    include Mongoid::Document
    ...
    ...
    has_many :pictures
    embeds_many :votos
end

But i don't know if this is correct. ¿Can i store the same model (in this case Voto) in two differents documents (Picture and User)?

Any idea how to achieve this?

matiasfha
  • 1,270
  • 4
  • 23
  • 42

1 Answers1

0

You can do this by using the polymorphic flag (see http://mongoid.org/en/mongoid/docs/relations.html#common Polymorphism)

class Voto
    include Mongoid::Document
    embedded_in :voteable, , polymorphic: true
    field :value, :type => Integer
end

class Picture
    include Mongoid::Document
    embeds_many :votos, as: :voteable
    embeds_many :comments
    belongs_to  :user
    ...
    ...
end

class User
    include Mongoid::Document
    ...
    ...
    has_many :pictures
    embeds_many :votos, as: :voteable
end
vchuravy
  • 1,208
  • 10
  • 22