views:

428

answers:

2

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
+2  A: 

If you're going to use self., use it on the method definition, not the call.

def self.create_deliverable(...)
    ...
end
...
create_deliverable("Low", type.id, phase.id)
...

It's my understanding that .rb files without a class definition get wrapped in an anonymous ruby class when they are run, so defining the method on self should work just fine.

localshred
+1 for the explanation about how `.rb` files without a class definition get wrapped in an anonymous ruby class
ahsteele
You are welcome. :)
localshred
+1  A: 

Looks to me like you placed your create_deliverable method after "private" access modifier in your script. Put it after public.

public

    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
private # to keep the rest of methods private
LymanZerga