Rails: get all field for an object coming from the database

后端 未结 1 1466
孤城傲影
孤城傲影 2021-02-07 18:50

Let\'s say I have an Foo ActiveRecord model with fields foo_id, foo_name and foo_description.

After doin

相关标签:
1条回答
  • 2021-02-07 19:31

    There is an attributes method that give a hash of field and values. So you could use

    @foo.attributes.keys
    

    To get an array of the fields on the model.

    There's also a Foo.column_names class method that gives you the same information without having to look up a record first.

    Documentation for:
    ActiveRecord::Base.column_names
    ActiveRecord::Base#attributes

    0 讨论(0)
提交回复
热议问题