Search code examples
ruby-on-rails-3consoleassociationsmodel-associations

How to check if a model's association is dependent with another


I want to debug a messy app and I want to see the effective model configuration

Is there a way to see what rails know about a model in terms of its associations?

for example - if I have

class User

has_many :comments, :dependent => :destroy

end

I'd like to see that rails know that it will call the destroy action in the comment controller if destroy action is called in users controller

Is there a way to see in rails console?


Solution

  • got it,

    it can be done with

    User.reflections
    

    which results in

     :comments=>
      #<ActiveRecord::Reflection::AssociationReflection:0xc52abc8
       @active_record=
        User(id: integer, ...)
       @collection=true,
       @macro=:has_many,
       @name=:comments,
       @options={:dependent=>:destroy, :extend=>[]},
       @plural_name="comments">
    

    which, as you can see, gives you the type of association (has_many), what it is called (:comments), the options and the plural name.

    I am in :love: with this method :)