Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

activerecord - How do you discover model attributes in Rails?

I am finding it difficult to easily see what attributes/properties exist on all of my model classes since they are not explicitly defined in my class files.

To discover model attributes, I keep the schema.rb file open and flip between it and whatever code I'm writing as needed. This works but is clunky because I have to switch between reading the schema file to pick up attributes, the model class file to check methods, and whatever new code that I'm writing to call attributes & methods.

My question is, how do you discover model attributes when you're analyzing a Rails codebase for the first time? Do you keep the schema.rb file open all the time, or is there a better way that doesn't involve jumping between schema file & model file constantly?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For Schema related stuff

Model.column_names         
Model.columns_hash         
Model.columns 

For instance variables/attributes in an AR object

object.attribute_names                    
object.attribute_present?          
object.attributes

For instance methods without inheritance from super class

Model.instance_methods(false)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...