mongoid inheritance problem
Asked Answered
R

1

7

I have a model like this

class Canvas
  include Mongoid::Document
  field :name
  referenced_in :hero
end

class Browser < Canvas
  field :version, :type => Integer
end

class Hero
  include Mongoid::Document
  field :name
  references_many :canvases
end

How can I build Brower Object refered from Hero object.

All i want to do is

h = Hero.create!({:name => 'Aston'})
h.browsers.build

However it gave me an error

undefined method `browsers' for #<Hero _id: 4d92c8fc1426960fff000005, name: "Aston">

Am i missing something?

Thanks

Repurchase answered 30/3, 2011 at 6:12 Comment(0)
M
4

try this

h.canvases.build({},Browser) 

this works for mongoid.2.0.0.rc7

Mchenry answered 30/3, 2011 at 6:40 Comment(2)
note this answer works only for Mongoid 2.0.0rc7 and doesn't work in mongoid-2.0.0.beta.19.Repurchase
I found this answer through modetojoy.blogspot.com/2010/01/inheritance-in-mongoid.htmlMchenry

© 2022 - 2024 — McMap. All rights reserved.