2013-04-03 45 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术语的键。

相关问题