2013-04-03 42 views
0

我剛接觸rails,但大多數文檔都是向用戶輸入視圖中的東西,並最終傳遞到數據庫中。無頭導軌,簡單的方法來存儲散列?

  1. 是否存在下面存儲到SQL數據庫的導軌方式?我把它放在模型或控制器中嗎?
  2. 有沒有一種乾淨的方式來存儲這些數據,還是我必須明確地存儲這個Hash中的每個屬性?
  3. 我已經手動進行了與大多數(如果不是全部)哈希數據匹配的手動遷移,但是有沒有可以將這些哈希轉換爲關係數據模型的工具?

{ 
"_id" : "36483f88e04d6dcb60684a33000791a6bc522a41", 
"address_components" : [ 
    { 
     "long_name" : "ON", 
     "short_name" : "ON", 
     "types" : [ 
      "administrative_area_level_1", 
      "political" 
     ] 
    }, 
    { 
     "long_name" : "CA", 
     "short_name" : "CA", 
     "types" : [ 
      "country", 
      "political" 
     ] 
    }, 
    { 
     "long_name" : "M5J 1L4", 
     "short_name" : "M5J 1L4", 
     "types" : [ 
      "postal_code" 
     ] 
    } 
], 
"formatted_address" : "ON, Canada", 
"formatted_phone_number" : "(416) 362-5221", 
"geometry" : { 
    "location" : { 
     "lat" : 43.640816, 
     "lng" : -79.381752 
    } 
}, 
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", 
"id" : "36483f88e04d6dcb60684a33000791a6bc522a41", 
"international_phone_number" : "+1 416-362-5221", 
"name" : "Scandinavian Airlines", 
"reference" : "CoQBcgAAAMobbidhAbzwIMkxq3GTHzzlEW4hAAwxg5EmGDP7ZOcJRwUK29poFMTDvED5KW9UEQrqtgTwESj_DuCAchy6Qe5pPZH9tB47MmmuQHvyHFlApunmU3MN05_KLekN5hEbrW7Gv2ys2oXmn7FpvD7-0N0QILlFXCiwL5UlYWo2sEg3EhBMBsrkHBu4WCFsMCHRqgadGhTM3BVWR15l9L87zL1uN1ssoW4WCw", 
"types" : [ 
    "restaurant", 
    "food", 
    "establishment" 
], 
"url" : "https://plus.google.com/100786723768255083253/about?hl=en-US", 
"utc_offset" : -300, 
"vicinity" : "" 

}

+1

您確定要使用ActiveRecord這個?看起來,對於這種類型的數據結構來說,NOSQL支持可能會更好 - 請看一下[Mongoid](http://mongoid.org/en/mongoid/)。 – PinnyM 2013-04-03 21:15:49

+0

其實來自Mongo。這很神奇,對於這個數據結構來說可能是完美的,但我需要將它與更大的應用程序集成,所以將其轉換爲SQL是有意義的。 – 2013-04-04 01:39:15

回答

0
  1. 該數據結構可以通過模型/關聯的匹配層次來存儲。
  2. 有這是一個非常乾淨的方式...
  3. 使用accepts_nested_attributes_for。這將適用於您的整個結構,但對於包含簡單字符串列表的'類型'數組,不起作用。但是,您可以針對此特定情況使用解決方法。

唯一不能存儲(容易)的是id。 ActiveRecord不允許你直接設置id,因爲它應該是後備數據庫的實現細節。在你的情況下,你可以簡單地借用似乎包含相同數據的_id字段,並將其插入某種別名中。

下面是代碼的一個例子,你可以使用:

class Address < ActiveRecord::Base 
    has_many :address_components 
    has_many :address_types 
    has_one :geometry 

    attr_accessor :address_components_attributes, :geometry_attributes 
    accepts_nested_attributes_for :address_components, :geometry 

    def types=(types) 
    types.each do |t| 
     self.address_types << AddressType.build(name: t) 
    end 
    end 

    def _id=(orig_id) 
    self.original_id = orig_id 
    end 
end 

class AddressType < ActiveRecord::Base 
    belongs_to :address 
end 

class Geometry < ActiveRecord::Base 
    belongs_to :address 
    has_one :location 

    attr_accessor :location_attributes 
    accepts_nested_attributes_for :location 
end 

class Location < ActiveRecord::Base 
    belongs_to :geometry 
end 

class AddressComponent < ActiveRecord::Base 
    belongs_to :address 
    has_many :component_types 

    def types=(types) 
    types.each do |t| 
     self.component_types << ComponentType.build(name: t) 
    end 
    end 
end 

class ComponentType < ActiveRecord::Base 
    belongs_to :address_component  
end 

現在你可以使用存儲整個結構:

Address.create(data_hash) 
0

如果你有你的模型setter方法,它們可以從這個哈希,你想處理數據的導入。

例如,上面給出的哈希值,如果你有一個方法:

def address_components=(ac) 
    # Handle address components 
end 

這將被調用,如果當你做以下(假設你的模型的名稱是爲MyModel和散列存儲在@hash)。

MyModel.new(@hash) 

所有的鍵都會觸發結構'key ='的setter方法。這是非常強大的 - 如果你有一個結構非常好的模型,但有一個任意的散列,你可以創建處理散列中的鍵的方法。基於這些,您可以構建新對象,構建關聯並同時保存所有對象。

請注意 - 您可能需要去掉一些鍵,或者處理一些按照自定義方式使用保留的ruby術語的鍵。

相關問題