Class: SmartSeeds::Generator::ForeignKey

Inherits:
Integer
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/smart_seeds/generator/integer/foreign_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(column, model) ⇒ ForeignKey

Returns a new instance of ForeignKey.



6
7
8
# File 'lib/smart_seeds/generator/integer/foreign_key.rb', line 6

def initialize(column, model)
  super
end

Instance Method Details

#foreign_key?Boolean

Returns:



21
22
23
# File 'lib/smart_seeds/generator/integer/foreign_key.rb', line 21

def foreign_key?
  true if associations_include_column?
end

#generate_valueObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/smart_seeds/generator/integer/foreign_key.rb', line 10

def generate_value
  klass = determine_klass

  if klass.ids.empty?
    raise ActiveRecord::ActiveRecordError,
          "There are no records in #{camelized_model_name}. You have to add those first. You can run SmartSeeds.plant(#{camelized_model_name})"
  else
    klass.ids.sample
  end
end