【问题标题】:Why am I getting two different sets of values from my Ruby object when using .to_hash?为什么我在使用 .to_hash 时会从我的 Ruby 对象中获得两组不同的值?
【发布时间】:2017-03-15 22:03:12
【问题描述】:

我的 Rails 项目中有一个对象,当我执行 record.to_hash 时,我得到了值,但我至少缺少一个 - 特别是 experience。当我执行record.attributes.to_hash 时,我会得到更多选项,包括属性experience。这是我在下面使用 byebug 的输出。谁能解释为什么会这样?我使用 record.to_hashupdate_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}

【问题讨论】:

    标签: ruby-on-rails ruby activerecord hash


    【解决方案1】:

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

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

    【讨论】:

    • 所以,如果我使用属性,除此之外,它还包括像id 这样的属性。有没有更多Rails 的方式来做我想做的事情,而无需通过额外的代码删除这些属性?不确定是否有我应该使用的方法。
    • @daveomcd record.attributes 已经是Hash,所以它你想要的:) 无需调用to_hashrecord.attributes == record.attributes.to_hash => true
    • 好的,谢谢,我想这就是我将与 update_attributes 一起使用的东西 -- record.attributes.except('id','created_at','updated_at')
    • @daveomcd 例如,您可以在Record 模型中实现to_h 方法,如下所示:def to_h; attributes.except('id','created_at','updated_at') end 并将其用作record.to_h
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多