Module: ActiveRecord::ConnectionAdapters::OracleEnhancedTableDefinition

Defined in:
lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb

Defined Under Namespace

Classes: ForeignKey

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 67

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method_chain :references, :foreign_keys
    alias_method_chain :to_sql, :foreign_keys

    def virtual(* args)
      options = args.extract_options!
      column_names = args
      column_names.each { |name| column(name, :virtual, options) }
    end

  end
end

Instance Method Details

#foreign_key(to_table, options = {}) ⇒ Object

Defines a foreign key for the table. to_table can be a single Symbol, or an Array of Symbols. See SchemaStatements#add_foreign_key

Examples
Creating a simple foreign key
t.foreign_key(:people)
Defining the column
t.foreign_key(:people, :column => :sender_id)
Creating a named foreign key
t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
Defining the column of the to_table.
t.foreign_key(:people, :column => :sender_id, :primary_key => :person_id)


117
118
119
120
121
122
123
124
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 117

def foreign_key(to_table, options = {})
  if @base.respond_to?(:supports_foreign_keys?) && @base.supports_foreign_keys?
    to_table = to_table.to_s.pluralize if ActiveRecord::Base.pluralize_table_names
    foreign_keys << ForeignKey.new(@base, to_table, options)
  else
    raise ArgumentError, "this ActiveRecord adapter is not supporting foreign_key definition"
  end
end

#lob_columnsObject



132
133
134
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 132

def lob_columns
  columns.select(&:lob?)
end

#raw(name, options = {}) ⇒ Object



63
64
65
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 63

def raw(name, options={})
  column(name, :raw, options)
end

#references_with_foreign_keys(*args) ⇒ Object

Adds a :foreign_key option to TableDefinition.references. If :foreign_key is true, a foreign key constraint is added to the table. You can also specify a hash, which is passed as foreign key options.

Examples
Add goat_id column and a foreign key to the goats table.
t.references(:goat, :foreign_key => true)
Add goat_id column and a cascading foreign key to the goats table.
t.references(:goat, :foreign_key => {:dependent => :delete})

Note: No foreign key is created if :polymorphic => true is used. Note: If no name is specified, the database driver creates one for you!



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 93

def references_with_foreign_keys(*args)
  options = args.extract_options!
  fk_options = options.delete(:foreign_key)

  if fk_options && !options[:polymorphic]
    fk_options = {} if fk_options == true
    args.each { |to_table| foreign_key(to_table, fk_options) }
  end

  references_without_foreign_keys(*(args << options))
end

#to_sql_with_foreign_keysObject

:nodoc:



126
127
128
129
130
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 126

def to_sql_with_foreign_keys #:nodoc:
  sql = to_sql_without_foreign_keys
  sql << ', ' << (foreign_keys * ', ') unless foreign_keys.blank?
  sql
end