1

この質問は素朴すぎるかもしれませんが、本当に混乱しました。これが私のコード、職業クラスです。

class Profession < OhmModel
include ProfessionMethods

attribute :type,  Type::Integer
attribute :level,  Type::Integer

attribute :add_point,  Type::Hash
attribute :attribute_add_point,  Type::Hash

reference :player, Player

def initialize(args = {})
  super
  if args.blank?
    self.type = 1
   self.level = 1
  end
  @add_point = SerializedHash.new
  @attribute_add_point = SerializedHash.new
end

# 初始化加点
def init_add_point(hash)
  hash.each do |key, value|
    @add_point[key] = value
  end
  update_add_point
end

def update_add_point
 self.add_point = @add_point
 self.save
 init_attribute_add_point
end

def init_attribute_add_point
  self.add_point.each do |key, value|
    key_new = key.to_s.gsub('_percent', '').to_sym
    PROFESSIONS[type][key_new][:attribute].split(',').each do |attr|
      @attribute_add_point[attr] = value
    end
  end
  update_attribute_add_point
end

def update_attribute_add_point
  self.attribute_add_point = @attribute_add_point
  self.save
end

def name
  PROFESSIONS[type][:name]
end


def probability
  hash = {}
  PROFESSIONS[type].keys.each do |key|
    case key  
    when /property_id_\d?_percent/        
      hash[key] = PROFESSIONS[type][key]     
    end  
  end
  return hash
end

def base_value
  hash = {}
  hash[:basic] = PROFESSIONS[type][:basic]
  return hash
end

def points_count
  5*level
end

def get_add_point
  hash  ={}
  add_point_hash = self.add_point
  add_point_hash.keys.each do |key|
    key_new = key.to_s.gsub('_percent', '').to_sym
    info = "#{PROFESSIONS[type][key_new][:name]}: #{PROFESSIONS[type][key_new][:des]}"
    hash[info] = add_point_hash[key]
  end
  return hash
end

def fresh_add_point
  probability_hash = probability
  base_value_hash = base_value
  points_count_value = points_count
  calculate_probability(probability_hash, base_value_hash, points_count_value)
end

def redistribute_add_point
  hash = {}
  probability_hash = {}
  probability.keys.each do |key|
    probability_hash[key] = 1.0/probability.count
  end
  base_value_hash = base_value
  points_count_value = points_count
  add_point_hash = calculate_probability(probability_hash, base_value_hash,   points_count_value)
  init_add_point(add_point_hash)
  add_point_hash.keys.each do |key|
    key_new = key.to_s.gsub('_percent', '').to_sym
    info = "#{PROFESSIONS[type][key_new][:name]}: #{PROFESSIONS[type][key_new][:des]}"
    hash[info] = add_point_hash[key]
  end
  return hash
end


protected

def after_create
  hash = fresh_add_point
  init_add_point(hash)
end
end

初期化すれば大丈夫です。

pro = Profession.create
=>  #<Profession:0x9504ffc
@_memo={},
@add_point=
{:property_id_1_percent=>104.0,
 :property_id_2_percent=>101.0,
 :property_id_3_percent=>100.0,
 :property_id_4_percent=>100.0,
 :property_id_5_percent=>100.0},
@attribute_add_point=
{"crystal_output"=>104.0,
 "tritium_output"=>104.0,
 "power_consume"=>101.0,
 "speed"=>100.0,
 "armor"=>100.0,
 "hp_damage"=>100.0,
 "armor_damage"=>100.0,
 "sheild_damage"=>100.0},
@attributes=
{:type=>1,
 :level=>1,
 :created_at=>1368756861,
 :updated_at=>1368756861,
 :id=>"34",
 :add_point=>
 {:property_id_1_percent=>104.0,
  :property_id_2_percent=>101.0,
  :property_id_3_percent=>100.0,
  :property_id_4_percent=>100.0,
  :property_id_5_percent=>100.0},
  :attribute_add_point=>
 {"crystal_output"=>104.0,
  "tritium_output"=>104.0,
  "power_consume"=>101.0,
  "speed"=>100.0,
  "armor"=>100.0,
  "hp_damage"=>100.0,
  "armor_damage"=>100.0,
  "sheild_damage"=>100.0}},
 @errors={},
 @id="34">

ハッシュ型の値を pro.add_point に再割り当てしてオブジェクトを保存すると、ここに問題があります。

 pro.add_point = {:one => 1, :two => 2}
 pro.save
 Profession[34]
 => #<Profession:0x9a544bc
 @_memo={},
 @add_point={},
 @attribute_add_point={},
 @attributes= 
   {:created_at=>"1368756861",
    :level=>"1",
    :attribute_add_point=>
         "{\"crystal_output\":104.0,\"tritium_output\":104.0,\"power_consume\":101.0,\"speed\":100.0,\"armor\":100.0,\"hp_damage\":100.0,\"armor_damage\":100.0,\"sheild_damage\":100.0}",
    :add_point=>"{:one=>1, :two=>2}",
    :updated_at=>"1368757147",
    :type=>"1"},
     @id="34">

「add_point」属性は通常の文字列として保存され、JSON スタイルに変換されません。

4

0 に答える 0