Pergunta

not sure what I'm doing wrong when adding an object to set, I'm getting the error undefined method 'bson_type' for #<Favorite:0x007ffadd59ba48> at the following line

favorite = Favorite.new(
        item_id: params[:item_id],
        item_type: params[:item_type],
        duid: params[:duid],
        name: params[:name]
)

profile.add_to_set(favorites: favorite)

models

class Favorite
  include Mongoid::Document
  embedded_in :profile
  field :item_id, type: String
  field :item_type, type: String
  field :name, type: String
  field :duid, type: String
end
class Profile
  include Mongoid::Document
  field :profile_id, type: String
  field :name, type: String
  field :image, type: String
  embeds_many :favorites
end
Foi útil?

Solução

One way to solve this issue is to use:

favorite = Favorite.new(item_id: params[:item_id], ... yada yada)
favorite.profile = profile
favorite.save!

Another way to solve this issue is to use the following code -- which I personally prefer:

profile.favorites.create!( item_id: params[:item_id], ...yada yada )

Extra Info can be ignored

class Profile
  ...
  embeds_many :favorites, cascade_callbacks: true
  ...
end

to allow the running of the callbacks found in the Favorite model

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top