2015-09-05 54 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