I read a few other posts on this subject and am still confused. In my seeds.rb I call delete and create on the model without any issues… when I get to the custom methods I created I get an undefined method error. The create and delete_all work fine when I comment out the name_gen and ssn_gen rows.
Also, this is Rails 3.1.1 on Ruby 1.8.7
Update: should have also mentioned I get the same issue if I change the create to new and move the name_gen sections to something like @sample_data_set.officialFirstName = SampleDataSet.name_gen
Error: undefined method `name_gen’ for #
Command for 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 model
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
In order to call some method directly on class like that:
instead of calling it on an instance of that class (as regular methods are called) like that:
you should define that method as a class method.
You can do it using
self.name_geninstead ofname_genin method definition like that: