【问题标题】:How do you output all attributes including attr_accessor attributes?你如何输出所有属性,包括 attr_accessor 属性?
【发布时间】:2012-05-08 08:59:19
【问题描述】:

假设我有一个包含列名称和电子邮件的用户类:

Class User < ActiveRecord::Base
  attr_accessor :gender
end

user = User.new(:gender => 'male', :name => 'joe', :email => 'user@example.com')

如果我想要属性,我通常可以使用 user.attributes、user.inspect 或 user.to_yaml。 但是,这不会输出性别。有没有办法可以轻松输出所有内容?

【问题讨论】:

标签: ruby-on-rails ruby activerecord attr-accessor


【解决方案1】:

我不相信你想要做的事情很容易做到。

看到这个问题:

How to iterate ActiveRecord Attributes, including attr_accessor methods

【讨论】:

  • 谢谢,您是搜索大师。我找不到为此发布的先前问题。我想我只需要手动编码所有 attr_accessor 字段。
  • 感谢您的来信。抱歉,我无法为您找到更具体的解决方案。我在这里也学到了一些东西,所以谢谢。你介意在某个时候接受答案吗?
猜你喜欢
  • 2010-11-02
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多