为什么在使用.to_hash时从Ruby对象获取两组不同的值?

时间:2016-11-02 14:31:59

标签: ruby-on-rails ruby activerecord hash

我的Rails项目中有一个对象,当我experience时,我得到了值,但我至少缺少一个 - 尤其是experience。当我执行record.attributes.to_hash时,我会获得更多选项,包括属性record.to_hash。这是我在下面使用byebug的输出。任何人都可以解释为什么会这样吗?我正在使用update_attributesrecord.to_hash #=> {:player_url=>"http://www.unlvrebels.com//sports/m-footbl/mtt/casey_acosta_1024286.html", :headshot_url=>nil, :jersey=>"87", :first_name=>"Casey", :last_name=>"Acosta", :position=>"WR", :height=>"5110", :weight=>"160", :eligibility=>nil, :hometown_city=>"Las Vegas", :hometown_state=>"NV", :high_school=>"Chaparral HS", :biography_text=>nil, :is_basketball_player=>false, :possible_dob=>false, :possible_transfer=>false, :possible_redshirt=>false, :possible_gpa=>false, :possible_track_participant=>false, :possible_basketball_participant=>false, :possible_baseball_participant=>false, :possible_hockey_participant=>false, :possible_lacrosse_participant=>false, :possible_power_lifting_participant=>false, :possible_rugby_participant=>false, :possible_soccer_participant=>false, :possible_volleyball_participant=>false, :possible_wrestling_participant=>false, :possible_medical_alert=>false, :possible_character_alert=>false, :school_id=>664, :player_id=>nil, :position_id=>2} record.attributes.to_hash #=> {"id"=>nil, "school_site_id"=>nil, "player_url"=>"http://www.unlvrebels.com//sports/m-footbl/mtt/casey_acosta_1024286.html", "headshot_url"=>nil, "jersey"=>"87", "first_name"=>"Casey", "last_name"=>"Acosta", "position"=>"WR", "height"=>"5110", "weight"=>"160", "eligibility"=>nil, "hometown_city"=>"Las Vegas", "hometown_state"=>"NV", "high_school"=>"Chaparral HS", "major"=>nil, "previous_school"=>nil, "experience"=>"FR", "biography_text"=>nil, "has_relative_in_sports"=>false, "is_basketball_player"=>false, "possible_dob"=>false, "possible_transfer"=>false, "possible_redshirt"=>false, "possible_gpa"=>false, "possible_track_participant"=>false, "possible_basketball_participant"=>false, "possible_baseball_participant"=>false, "possible_hockey_participant"=>false, "possible_lacrosse_participant"=>false, "possible_power_lifting_participant"=>false, "possible_rugby_participant"=>false, "possible_soccer_participant"=>false, "possible_volleyball_participant"=>false, "possible_wrestling_participant"=>false, "possible_medical_alert"=>false, "possible_character_alert"=>false, "school_id"=>664, "player_id"=>nil, "position_id"=>2, "created_at"=>nil, "updated_at"=>nil} 方法一起更新对象,但我需要其他属性。

{{1}}

1 个答案:

答案 0 :(得分:3)

recordRecord类的实例,而record.attributesHash类的实例。

可能会出现差异,因为to_hash方法对这两个类的实例的实现方式不同。