Thursday 25 July 2013

Viewing associations of a particular model

Today i would like to share about viewing associations of a particular model together in console.
How to get rails associations from console

  1) User Model associate with UserDetail and Photo Model

     class User < ActiveRecord::Base
       has_one :user_detail
       has_many :photos
    end

  2) Open your rails console

     User.reflect_on_all_associations.map{ |reflection| ":#{reflection.macro} => :#{reflection.name}"}

     => [":has_one => :user_detail",":has_many => :photos"]



No comments:

Post a Comment