我的Rails项目中有一个对象,当我experience
时,我得到了值,但我至少缺少一个 - 尤其是experience
。当我执行record.attributes.to_hash时,我会获得更多选项,包括属性record.to_hash
。这是我在下面使用byebug的输出。任何人都可以解释为什么会这样吗?我正在使用update_attributes
与record.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}}
答案 0 :(得分:3)
record
是Record
类的实例,而record.attributes
是Hash
类的实例。
可能会出现差异,因为to_hash
方法对这两个类的实例的实现方式不同。