2015-09-05 53 views
0

的embeds_many场我在模型(mongoid)embeds_many场:items如何添加元素mongoid模型

class Course 
    embeds_many :items 

    def create_item 
    item = Item.new 
    update_attributes items, items | [item] 
    end 
end 

...

c = Course.new 
item = c.create_item 

这是正确的方式在:items添加新的元素?我尝试使用update_attributes items, items | [item],但我认为这是丑陋的方法。

回答

1

embeds_many支持常用的方法集的ActiveRecord的has_many这样做,你可以说这样的话:

def create_item 
    items.create(args) 
end 

就像你用AR has_many