Rails seeds.rb自定义方法的未定义方法

时间:2011-12-26 18:10:37

标签: ruby-on-rails ruby-on-rails-3

我读了一些关于这个主题的其他帖子,我仍然感到困惑。在我的seeds.rb中我调用了删除并在模型上创建没有任何问题...当我到达我创建的自定义方法时,我得到一个未定义的方法错误。当我注释掉create和ssn_gen行时,delete_allname_gen正常工作。

此外,这是Ruby 1.8.7上的Rails 3.1.1

更新:应该也提到如果我将create更改为new并将name_gen部分移动到@sample_data_set.officialFirstName = SampleDataSet.name_gen

之类的内容,我会遇到同样的问题

错误:#

的未定义方法`name_gen'

rake命令:bundle exec rake db:seed RAILS_ENV=development --trace

seeds.rb

SampleDataSet.delete_all

@sample_data_set = SampleDataSet.new (
      :campusNum               => "96",
      :dateOfBirth             => "1981-10-09",
      :gender                  => "M",
      :officialMiddleInitial   => "L",
      :addressLine1            => "PO BOX 9",
      :addressLine2            => "", 
      :city                    => "WOODLAND",
      :state                   => "GA",
      :zipCode                 => "31836",
      :homeAreaCode            => "706",
      :homePhoneNumber         => "6742435",
      :homePhoneCountryCode    => "US",
      :workAreaCode            => "706",
      :workPhoneNumber         => "6742435",
      :workPhoneCountryCode    => "US",
      :usCitizen               => true,
      :financialAid            => true,
      :previousDegree          => "ADN",
      :region                  => "MAIN",
      :program                 => "AAPSY",
      :version                 => "012",
      :team                    => "TEAM 3236A",
      :enrollmentUserId        => "SSGROTH",
      :revCampusOid            => "1627",
      :executingUserId         => "QROBINSO",
      :totalDeclaredExtCredits => "1",

      #generating some default values for the gen fields... except IRN
      :officialFirstName => SampleDataSet.name_gen,
      :officialLastName => SampleDataSet.name_gen,          
      :enrollAgreeSignDate => Date.today.strftime('%Y-%m-%d'),
      :scheduledStartDate => Date.tomorrow.strftime('%Y-%m-%d'),
      :ssn => SampleDataSet.ssn_gen.to_s
      )

@sample_data_set.emailAddresses = officialFirstName + "." + officialLastName + "@aaaa.phoenix.edu"

SampleDataSet模型

class SampleDataSet < ActiveRecord::Base

  #Random info generation
  def name_gen(*prepend)
    #Random character generation piece I found on Stackoverflow with 102 upvotes
    character_map =  [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
    name  =  (0..8).map{ character_map[rand(character_map.length)]  }.join

    if prepend.nil?
      return name
    else
      return prepend.to_s + "_" + name
    end
  end

  def ssn_gen
    #broke this out as its own method in case someone wants some logic later on
    ssn = Random.rand(1000000000) + 99999999
    return ssn
  end

end

1 个答案:

答案 0 :(得分:3)

为了直接在类上调用某些方法:

SampleDataSet.name_gen

而不是在那个类的实例上调用它(就像调用常规方法一样):

a = SampleDataSet.new
a.name_gen 

您应该将该方法定义为类方法。

您可以在方法定义中使用self.name_gen代替name_gen来执行此操作:

class SampleDataSet < ActiveRecord::Base

  # Random info generation
  def self.name_gen(*prepend)
    # Random character generation piece I found on Stackoverflow with 102 upvotes
    character_map = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
    name = (0..8).map { character_map[rand(character_map.length)] }.join

    if prepend.nil?
      return name
    else
      return prepend + "_" + name
    end
  end

  def self.ssn_gen
    # broke this out as its own method in case someone wants some logic later on
    ssn = Random.rand(1000000000) + 99999999
    return ssn
  end

end