Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoid inheritance problem

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

like image 367
Gagan Avatar asked Mar 30 '11 06:03

Gagan


1 Answers

try this

h.canvases.build({},Browser) 

this works for mongoid.2.0.0.rc7

like image 76
Sadiksha Gautam Avatar answered Sep 30 '22 07:09

Sadiksha Gautam