0

I've just realized that aside from checking the database or reading all the migrations that contributed to a certain model, I'm not really sure how I would find out what attributes a certain Model had in a Rails project. In any other context I've ever worked in, I would go look at the file where the Model definition was (the class file, per se) to find out something like that.

How is this normally done in Rails when you join a pre-existing project and are learning the code? Just by using the Rails console?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • `db/schema.rb` shows your database tables. For your second question I guess this is the same with all MVC frameworks: reading models and controllers and maybe some help from a team member for big apps with tens of models – Maxence Jun 09 '18 at 10:17
  • Awesome, I didn't know about it! Very new to rails, sorry for the beginner question. Thank you. – temporary_user_name Jun 09 '18 at 10:18
  • 1
    It is still useful to have PGadmin open. Also even if I work with Sublime IDE, Rubymine has a tab that shows all database tables with their content. pretty useful – Maxence Jun 09 '18 at 10:20
  • you can read this answer too https://stackoverflow.com/questions/1289557/how-do-you-discover-model-attributes-in-rails – eth3rnit3 Jun 09 '18 at 13:09

1 Answers1

1

I will suggest you to use "Annotate", this gem automatically add a structured comment in you model(fixtures, factories...) when you make a migration, like this:

# == Schema Info
#
# Table name: line_items
#
#  id                  :integer(11)    not null, primary key
#  quantity            :integer(11)    not null
#  product_id          :integer(11)    not null
#  unit_price          :float
#  order_id            :integer(11)
#

 class LineItem < ActiveRecord::Base
   belongs_to :product

Remember to add this gem in the development group:

group :development do
  gem 'annotate'
end

and run this command rails g annotate:install to create a rake task. The rake task will be called at every migration

Patrick Barattin
  • 617
  • 6
  • 18
  • Great suggestion, thanks! – temporary_user_name Jun 09 '18 at 13:27
  • Secondary question though, I'm not familiar with the console syntax you're using there. I thought you had to run something like `rails g task` and then fill out the file by hand. Where can I see documentation on the way you're doing it there, which I don't really understand? – temporary_user_name Jun 09 '18 at 13:36
  • 1
    `rails g annotate:install` will run their custom install file. You can get more info here: https://github.com/ctran/annotate_models/blob/develop/README.rdoc#configuration-in-rails – Patrick Barattin Jun 09 '18 at 16:43