Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中获取ActiveRecord中对象的所有值?_Ruby On Rails_Ruby_Database_Rails Activerecord - Fatal编程技术网

Ruby on rails 如何在Rails中获取ActiveRecord中对象的所有值?

Ruby on rails 如何在Rails中获取ActiveRecord中对象的所有值?,ruby-on-rails,ruby,database,rails-activerecord,Ruby On Rails,Ruby,Database,Rails Activerecord,可以使用column\u names获取表中的所有列,但如果希望了解ActiveRecord中特定实例的所有值,该怎么办 比如说, User.column_names => ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign

可以使用
column\u names
获取表中的所有列,但如果希望了解ActiveRecord中特定实例的所有值,该怎么办

比如说,

User.column_names
=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "tel", "interests", "admin_status", "created_at", "updated_at", "city_id", "profile_image_file_name", "profile_image_content_type", "profile_image_file_size", "profile_image_updated_at"]

User.first
=> #<User id: 1, email: "aatir@jpl.com", tel: "+923223333333", interests: "Hi, I'm interested in coding.", admin_status: "download", created_at: "2016-08-16 22:38:05", updated_at: "2016-08-16 22:38:05", city_id: 2, profile_image_file_name: "Screen_Shot_2016-07-02_at_4.41.32_PM.png", profile_image_content_type: "image/png", profile_image_file_size: 324372, profile_image_updated_at: "2016-08-16 22:38:04">
User.column\u名称
=>[“id”、“电子邮件”、“加密密码”、“重置密码令牌”、“重置密码发送时间”、“记住创建时间”、“登录次数”、“当前登录时间”、“最后登录时间”、“当前登录ip”、“最后登录ip”、“最后登录ip”、“电话”、“兴趣”、“管理员状态”、“创建时间”、“更新时间”、“城市id”、“档案图像文件名”、“档案图像内容类型”,“配置文件图像文件大小”,“配置文件图像更新位置”]
用户优先
=> #

当调用
第一个
方法时,它返回键值对。我想要的是只有值,ActiveRecord中是否有一个方法可以为我们这样做?

只需将ActiveRecord转换为哈希,然后按如下方式获取值:

User.first.as_json.values

您还可以使用
属性
方法来获取它

User.first.attributes.values
  • User.first.attributes
    将返回
    User
    对象的哈希表示

    User.first.attributes
    
    { 
      "id" => 1,
      "email" => "aatir@jpl.com",
      "tel" => "+923223333333",
      "interests" => "Hi, I'm interested in coding.",
      "admin_status" => "download",
      "created_at" => "2016-08-16 22:38:05",
      "updated_at" => "2016-08-16 22:38:05",
      "city_id" => 2,
      "profile_image_file_name" => "Screen_Shot_2016-07-02_at_4.41.32_PM.png",
      "profile_image_content_type" => "image/png",
      "profile_image_file_size" => 324372,
      "profile_image_updated_at" => "2016-08-16 22:38:04"
    }
    
  • 将返回
    数组中的值

    User.first.attributes.values
    
    [1, "aatir@jpl.com", "+923223333333", "Hi, I'm interested in coding.", "download", "2016-08-16 22:38:05", "2016-08-16 22:38:05", 2, "Screen_Shot_2016-07-02_at_4.41.32_PM.png", "image/png", 324372, "2016-08-16 22:38:04"]
    

  • 我正在扩展@deepak的答案,以确保顺序与
    列名
    的顺序相同,我就是这样做到的:

    User.first.attributes.values_at *User.column_names
    
    要仅获取值,而不考虑顺序,请执行以下操作:

    User.first.attributes.values 
    

    您应该等待大约10分钟:D…谢谢:)我删除了我的答案以支持您的答案。但是我不确定
    版本是否有用,因为
    属性
    哈希中无法保证属性的顺序。如果哈希中没有固定的确定顺序,您无法确定数组。这将使以后很难从该数组中实际读取属性。@spickermann在某种程度上是确定性的。因为ruby 1.9值的返回顺序与它们添加到哈希中的顺序相同。我认为这是相对保存(但不保证)在Rails中,每个属性每次都以相同的顺序添加到属性散列中。这可能取决于数据库引擎,除非Rails在将值添加到属性散列中之前对列名进行排序。因为例如MySQL不保证任何顺序(除非您自己指定顺序)我怀疑结果是否具有确定性。
    
    User.first.attributes.values