2015-09-09 72 views
1
class Student < CouchRest::Model::Base 
    collection_of :phones 
    def add_phone(new_phone) 
    #this does not work 
    #new_phone_ids_list = phone_ids << new_phone.id 
    #this works 
    new_phone_ids_list = phone_ids + [new_phone.id] 
    self.update_attributes(:phone_id => new_phone_ids_list) 
    end 
end 

所不同的是,當我使用phone_ids < < new_phone.id創建一個新的列表,並更新CouchDB的,這是行不通的。但是,如果我使用phone_ids + [new_phone.id],更新將起作用。我明白phone_ids + [new_phone.id]會返回一個新的實例,但無法弄清楚,爲什麼phone_ids << new_phone.id不工作無法更新軌道模型

+0

「到收集IDS屬性(group_ids)所做的任何手動更改,除非更換,將需要CollectionOfProxy爲兩組數據的重新加載到保持同步的」 http://www.rubydoc。 info/github/couchrest/couchrest_model/CouchRest%2FModel%2FAssociations%2FlassMethods%3Acollection_of – bumpy

+0

@bumpy非常感謝!這正是我正在尋找的! – user2569579

+0

@bumpy我不知道如何將答案標記爲正確答案。 – user2569579

回答

0

嘗試直接使用散列補充:

new_phone_ids_list << new_phone.id 
self.update_attributes(:phone_id => new_phone_ids_list) 
+0

感謝您的回答。我試過了,它不起作用。 'new_phone_ids_list = phone_ids << new_phone.id'得到的結果相同。 – user2569579