Using a method in seeds.rb in Ruby On Rails

ahsteele picture ahsteele · Nov 17, 2009 · Viewed 8k times · Source

I am trying to add a method to my seeds.rb so that I don't have to write a bunch of verbose code. However, depending on the placement of the create_deliverable method I get one of two error messages when running db:setup.

When method is before call

rake aborted! private method 'create_deliverable' called for #

When method is after call

rake aborted! undefined method `create_deliverable' for #

Is it not possible to uses methods in seeds.rb? Am I somehow calling the method incorrectly (I've tried calling with and without the self.)?

Method

def create_deliverable(complexity, project_phase_id, deliverable_type_id)
  Deliverable.create(:name => (0...8).map{65.+(rand(25)).chr}.join,
      :size => 2 + rand(6) + rand(6),
      :rate => 2 + rand(6) + rand(6),
      :deliverable_type_id => deliverable_type_id,
      :project_phase_id => project_phase_id,
      :complexity => complexity)
end

Calling Code

@wf_project.project_phases.each do |phase|
  DeliverableType.find_by_lifecycle_phase(phase.lifecycle_phase_id).each do
    |type|
    self.create_deliverable("Low", type.id, phase.id)

    self.create_deliverable("Medium", type.id, phase.id)

    self.create_deliverable("High", type.id, phase.id)
  end
end

Answer

Joe Stepowski picture Joe Stepowski · Jul 23, 2015

Make sure you define the method before calling it:

def test_method
  puts "Hello!"
end

test_method