Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14019

Embedded document cannot hold other model IDs ? undefined method `bson_type'

Either I still haven't understood the limitations of embedded documents, or it's a bug :

I want my notifications to have a reference to a "sender" user model. However, I don't care about being able to find the notifications sent by a given user. I thought this setup would work :

EDIT : the problem is coming from my custom setter

class User
  embeds_many :notifications

class Notification
  embedded_in :user
  belongs_to :sender, class_name: "User", inverse_of: nil

  def sender=(someone)
    self[:sender] = (someone.is_a?(User) ? someone : someone.user)
  end

Now in the console :

user_a.notifications.create!(sender: user_b)

Throws a

NoMethodError: undefined method `bson_type' for #<User:0x83dd590>

Upvotes: 0

Views: 166

Answers (2)

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14019

I want to kill myself now...

def sender=(someone)
  self[:sender] = (someone.is_a?(User) ? someone : someone.user).id
end

Upvotes: 1

evanbikes
evanbikes

Reputation: 4171

This might help, might just be an observation.

sender= is overwriting the Rails method, so you might be getting some weirdness from that. I would do:

def sender=(someone)
  someone = someone.is_a?(User) ? someone : someone.user
  super(someone)
end

Hope that helps!

Upvotes: 0

Related Questions