2014年4月23日 星期三

t.belongs_to in migration


reference http://stackoverflow.com/questions/9471091/t-belongs-to-in-migration

auto create  *_id , *_type
       # Adds a reference. Optionally adds a +type+ column, if <tt>:polymorphic</tt> option is provided.
      # <tt>references</tt> and <tt>belongs_to</tt> are acceptable.
      # ===== Examples
      # t.references(:goat)
      # t.references(:goat, :polymorphic => true)
      # t.belongs_to(:goat)
      def references(*args)
        options = args.extract_options!
        polymorphic = options.delete(:polymorphic)
        args.each do |col|
          @base.add_column(@table_name, "#{col}_id", :integer, options)
          @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
        end
      end
      alias :belongs_to :references

Polymorphic Associations

Polymorphic Association Diagram

沒有留言:

張貼留言