I have a model in my Rails application - User. I want all the associations to be listed in rails console, along with the type of association(1-1, 1-many).
Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .
To exit the console, type: quit .
User.reflect_on_all_associations
This will return an array of associations similar to this:
#<ActiveRecord::Reflection::AssociationReflection:0x00000105575548 @macro=:has_many, @name=:posts, @options={}, @active_record=User(id: integer, login: string), @collection=false>
Sample code:
reflections = User.reflect_on_all_associations reflections.each do |reflection| puts ":#{reflection.macro} => :#{reflection.name}" end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With