Rails4升级:活动记录.to_yaml已更改

时间:2015-12-01 01:44:14

标签: ruby-on-rails

我将我的应用程序升级到Rails4。

我喜欢在控制台中执行puts object.to_yaml来检查我的对象。

在Rails 3中,我会得到:

puts User.first.to_yaml
--- !ruby/object:User
  id: '1'
  fname: Hello
  lname: World
  email: hello@world.com
  slug: hello-world
  facebook_url: 
  twitter_url: 
  skype_pseudo: 
  website_url: 
  created_at: '2015-06-23 08:20:35.744329'
  updated_at: '2015-08-18 14:54:25.669312'
  encrypted_password: "$2a$10$FDZgUOmxZCN5dYYCsMO1HuOfe.QfRsFd8XCnGV0DsiIMIwKDR4zoS"
  reset_password_token: 
  reset_password_sent_at: 
  remember_created_at: 
  sign_in_count: '1'
  current_sign_in_at: '2015-08-18 14:54:25.667587'
  last_sign_in_at: '2015-08-18 14:54:25.667587'
  current_sign_in_ip: 127.0.0.1
  last_sign_in_ip: 127.0.0.1

在Rails 4中,我得到了所有这些(对我来说没用)数据:

puts User.first.to_yaml
--- !ruby/object:User
raw_attributes:
  id: '1'
  fname: Hello
  lname: World
  email: hello@world.com
  slug: hello-world
  facebook_url: 
  twitter_url: 
  skype_pseudo: 
  website_url: 
  created_at: '2015-06-23 08:20:35.744329'
  updated_at: '2015-08-18 14:54:25.669312'
  encrypted_password: "$2a$10$FDZgUOmxZCN5dYYCsMO1HuOfe.QfRsFd8XCnGV0DsiIMIwKDR4zoS"
  reset_password_token: 
  reset_password_sent_at: 
  remember_created_at: 
  sign_in_count: '1'
  current_sign_in_at: '2015-08-18 14:54:25.667587'
  last_sign_in_at: '2015-08-18 14:54:25.667587'
  current_sign_in_ip: 127.0.0.1
  last_sign_in_ip: 127.0.0.1
attributes: !ruby/object:ActiveRecord::AttributeSet
  attributes: !ruby/object:ActiveRecord::LazyAttributeHash
    types:
      id: &2 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer
        precision: 
        scale: 
        limit: 
        range: !ruby/range
          begin: -2147483648
          end: 2147483648
          excl: true
      fname: &1 !ruby/object:ActiveRecord::Type::String
        precision: 
        scale: 
        limit: 255
      lname: *1
      email: *1
      slug: *1
      home_space_id: *2
      facebook_url: *1
      twitter_url: *1
      skype_pseudo: *1
      website_url: *1
      created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: &3 !ruby/object:ActiveRecord::ConnectionAdapters::PostgreSQL::OID::DateTime
          precision: 
          scale: 
          limit: 
      updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *3
      encrypted_password: *1
      reset_password_token: *1
      reset_password_sent_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *3
      remember_created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *3
      sign_in_count: *2
      current_sign_in_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *3
      last_sign_in_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
        subtype: *3
      current_sign_in_ip: *1
      last_sign_in_ip: *1
    values:
      id: '1'
      fname: Hello
      lname: World
      email: hello@world.com
      slug: hello-world
      facebook_url: 
      twitter_url: 
      skype_pseudo: 
      website_url: 
      created_at: '2015-06-23 08:20:35.744329'
      updated_at: '2015-08-18 14:54:25.669312'
      encrypted_password: "$2a$10$FDZgUOmxZCN5dYYCsMO1HuOfe.QfRsFd8XCnGV0DsiIMIwKDR4zoS"
      reset_password_token: 
      reset_password_sent_at: 
      remember_created_at: 
      sign_in_count: '1'
      current_sign_in_at: '2015-08-18 14:54:25.667587'
      last_sign_in_at: '2015-08-18 14:54:25.667587'
      current_sign_in_ip: 127.0.0.1
      last_sign_in_ip: 127.0.0.1
    additional_types: {}
    materialized: true
    delegate_hash:
      id: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: id
        value_before_type_cast: '1'
        type: *2
      fname: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: fname
        value_before_type_cast: Hello
        type: *1
      lname: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: lname
        value_before_type_cast: World
        type: *1
      email: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: email
        value_before_type_cast: hello@world.com
        type: *1
      slug: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: slug
        value_before_type_cast: hello-world
        type: *1
      facebook_url: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: facebook_url
        value_before_type_cast: 
        type: *1
      twitter_url: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: twitter_url
        value_before_type_cast: 
        type: *1
      skype_pseudo: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: skype_pseudo
        value_before_type_cast: 
        type: *1
      website_url: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: website_url
        value_before_type_cast: 
        type: *1
      created_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: created_at
        value_before_type_cast: '2015-06-23 08:20:35.744329'
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      updated_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: updated_at
        value_before_type_cast: '2015-08-18 14:54:25.669312'
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      encrypted_password: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: encrypted_password
        value_before_type_cast: "$2a$10$FDZgUOmxZCN5dYYCsMO1HuOfe.QfRsFd8XCnGV0DsiIMIwKDR4zoS"
        type: *1
      reset_password_token: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: reset_password_token
        value_before_type_cast: 
        type: *1
      reset_password_sent_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: reset_password_sent_at
        value_before_type_cast: 
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      remember_created_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: remember_created_at
        value_before_type_cast: 
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      sign_in_count: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: sign_in_count
        value_before_type_cast: '1'
        type: *2
      current_sign_in_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: current_sign_in_at
        value_before_type_cast: '2015-08-18 14:54:25.667587'
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      last_sign_in_at: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: last_sign_in_at
        value_before_type_cast: '2015-08-18 14:54:25.667587'
        type: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
          subtype: *3
      current_sign_in_ip: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: current_sign_in_ip
        value_before_type_cast: 127.0.0.1
        type: *1
      last_sign_in_ip: !ruby/object:ActiveRecord::Attribute::FromDatabase
        name: last_sign_in_ip
        value_before_type_cast: 127.0.0.1
        type: *1
new_record: false
active_record_yaml_version: 0

任何改变我都会回到之前的行为或找到解决方法吗?

2 个答案:

答案 0 :(得分:1)

这对你有用吗?

User.first.attributes.to_yaml

答案 1 :(得分:1)

要更改to_yaml的输出,您可以覆盖encode_with。从Rails 4.1到4.2的不兼容性升级我遇到了同样的问题,并修复了:

class ActiveRecord::Base
  def encode_with(coder)
    coder['attributes'] = attributes_before_type_cast
  end
end

attributes是YAML中的关键字,attributes_before_type_cast是值。为了匹配问题中的请求(记录的属性直接位于YAML的根目录下),这应该有效:

class ActiveRecord::Base
  def encode_with(coder)
    attributes_before_type_cast.each{|k, v| coder[k] = v}
  end
end