Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How do I update a child db table when populating database in ruby on ralls?
- namespace :db do
- desc "Create user records in the development database."
- task :populate => :environment do
- require 'faker'
- def randomDate(params={})
- years_back = params[:year_range] || 5
- latest_year = params [:year_latest] || 0
- year = (rand * (years_back)).ceil + (Time.now.year - latest_year - years_back)
- month = (rand * 12).ceil
- day = (rand * 31).ceil
- series = [date = Time.local(year, month, day)]
- if params[:series]
- params[:series].each do |some_time_after|
- series << series.last + (rand * some_time_after).ceil
- end
- return series
- end
- date
- end
- def decimal_selection_array(start,limit,step_size=1)
- decimal_array = (start..limit).step(step_size).map{|i| i.to_s}.to_a
- decimal_array.insert(0,"Below #{start.to_f}")
- decimal_array.insert(-1,"Above #{limit.to_f}")
- end
- 100.times do |n|
- username = "#{Faker::Name.first_name}#{n}"
- u = User.create!(
- :username => username,
- :email => Faker::Internet.email,
- :password => "foobar"
- )
- u.profile.update_attributes(
- :motd => Faker::Lorem.words,
- #Profile details
- :first_name => Faker::Name.first_name,
- :last_name => Faker::Name.last_name,
- :birthday => randomDate(:year_range => 60, :year_latest => 22),
- :gender => (1..2).to_a.sample,
- :marital_status => (1..7).to_a.sample,
- :sexual_preference => (1..3).to_a.sample,
- :ethnicity => (1..10).to_a.sample,
- :country => Faker::Address.country,
- :location => Faker::Address.country,
- #About the user
- :about_me => Faker::Lorem.paragraph,
- #Personal stats
- :height => decimal_selection_array(5.0,7.0,0.1).to_a.sample,
- :body_type => (1..7).to_a.sample,
- :eye_colour => (1..6).to_a.sample,
- :drugs => (1..4).to_a.sample,
- :alcohol => (1..4).to_a.sample,
- :cigarettes => (1..3).to_a.sample,
- :likes => Faker::Lorem.sentence,
- :dislikes => Faker::Lorem.sentence,
- :bad_habits => Faker::Lorem.sentence,
- #Favourite things
- :food => Faker::Lorem.sentence,
- :music => Faker::Lorem.sentence,
- :television => Faker::Lorem.sentence,
- :book => Faker::Lorem.sentence,
- :animal => Faker::Lorem.sentence,
- :place => Faker::Lorem.sentence,
- :possesion => Faker::Lorem.sentence
- )
- end
- end
- end
- before_safe :build_profile
- # before running this task comment out: before_create :build_profile in user.rb in order to make this work correctly
- namespace :db do
- desc "Create user records in the development database."
- task :populate => :environment do
- require 'faker'
- def randomDate
- "#{(1900..2012).to_a.sample}-#{(1..12).to_a.sample}-#{(1..28).to_a.sample}"
- end
- def decimal_selection_array(start,limit,step_size=1)
- decimal_array = (start..limit).step(step_size).map{|i| i.to_s}.to_a
- decimal_array.insert(0,"Below #{start.to_f}")
- decimal_array.insert(-1,"Above #{limit.to_f}")
- end
- 1000.times do |n|
- username = "#{Faker::Name.first_name}#{n+1}"
- User.create!(
- :username => username.gsub(/[^0-9a-z]/i, ''),
- :email => Faker::Internet.email,
- :password => "foobar"
- )
- Profile.create!(
- :user_id => "#{n+1}",
- :motd => Faker::Lorem.sentence,
- #Profile details
- :first_name => Faker::Name.first_name.gsub(/[^a-z]/i, ''),
- :last_name => Faker::Name.last_name.gsub(/[^a-z]/i, ''),
- :birthday => randomDate,
- :gender => (1..2).to_a.sample,
- :marital_status => (1..7).to_a.sample,
- :sexual_preference => (1..3).to_a.sample,
- :ethnicity => (1..10).to_a.sample,
- :country => Faker::Address.country,
- :location => Faker::Address.country,
- #About the user
- :about_me => Faker::Lorem.paragraph,
- #Personal stats
- :height => decimal_selection_array(5.0,7.0,0.1).to_a.sample,
- :body_type => (1..7).to_a.sample,
- :hair => (1..7).to_a.sample,
- :eye_colour => (1..6).to_a.sample,
- :drugs => (1..4).to_a.sample,
- :alcohol => (1..4).to_a.sample,
- :cigarettes => (1..3).to_a.sample,
- :likes => Faker::Lorem.sentence,
- :dislikes => Faker::Lorem.sentence,
- :bad_habits => Faker::Lorem.sentence,
- #Favourite things
- :food => Faker::Lorem.sentence,
- :sport => Faker::Lorem.sentence,
- :music => Faker::Lorem.sentence,
- :television => Faker::Lorem.sentence,
- :book => Faker::Lorem.sentence,
- :animal => Faker::Lorem.sentence,
- :place => Faker::Lorem.sentence,
- :possession => Faker::Lorem.sentence
- )
- end
- end
- end
Add Comment
Please, Sign In to add comment