2012-08-11 26 views
1

我有一個字段「位置」,它是一個數組[lng,lat]。無法在ActiveAdmin和Mongoid 3.x中保存自定義字段(陣列)

我在ActiveAdmin形成兩個輸入字段,定義如下

f.inputs :name => "Location" do 
    f.input :latitude 
    f.input :longitude 
end 

爲了獲得緯度和經度我已經在我的模型定義了兩個干將:

def latitude 
    location[1] 
end 

def longitude 
    location[0] 
end 

形式是如預期所示。

爲了節省我已經創建了兩個二傳手的價值模型

def latitude=(lat) 
    self[:location][1] = lat.to_f 
end 

def longitude=(lon) 
    self[:location][0] = lon.to_f 
end 

表格後提交這些方法被調用,但值不會持久。

我想念什麼?

回答

0

我找到了解決方案。當我使用set()時,它工作。保存和update_attribute - 沒有。

def latitude=(lat) 
    self[:location][1] = lat.to_f 
    self.set :location, self[:location] 
end 

def longitude=(lon) 
    self[:location][0] = lon.to_f 
    self.set :location, self[:location] 
end