Guest User

Untitled

a guest
Aug 5th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. How do I update a child db table when populating database in ruby on ralls?
  2. namespace :db do
  3. desc "Create user records in the development database."
  4. task :populate => :environment do
  5. require 'faker'
  6.  
  7.  
  8. def randomDate(params={})
  9. years_back = params[:year_range] || 5
  10. latest_year = params [:year_latest] || 0
  11. year = (rand * (years_back)).ceil + (Time.now.year - latest_year - years_back)
  12. month = (rand * 12).ceil
  13. day = (rand * 31).ceil
  14. series = [date = Time.local(year, month, day)]
  15. if params[:series]
  16. params[:series].each do |some_time_after|
  17. series << series.last + (rand * some_time_after).ceil
  18. end
  19. return series
  20. end
  21. date
  22. end
  23.  
  24. def decimal_selection_array(start,limit,step_size=1)
  25. decimal_array = (start..limit).step(step_size).map{|i| i.to_s}.to_a
  26. decimal_array.insert(0,"Below #{start.to_f}")
  27. decimal_array.insert(-1,"Above #{limit.to_f}")
  28. end
  29.  
  30. 100.times do |n|
  31. username = "#{Faker::Name.first_name}#{n}"
  32. u = User.create!(
  33. :username => username,
  34. :email => Faker::Internet.email,
  35. :password => "foobar"
  36. )
  37.  
  38.  
  39. u.profile.update_attributes(
  40. :motd => Faker::Lorem.words,
  41.  
  42. #Profile details
  43. :first_name => Faker::Name.first_name,
  44. :last_name => Faker::Name.last_name,
  45. :birthday => randomDate(:year_range => 60, :year_latest => 22),
  46. :gender => (1..2).to_a.sample,
  47. :marital_status => (1..7).to_a.sample,
  48. :sexual_preference => (1..3).to_a.sample,
  49. :ethnicity => (1..10).to_a.sample,
  50. :country => Faker::Address.country,
  51. :location => Faker::Address.country,
  52.  
  53. #About the user
  54. :about_me => Faker::Lorem.paragraph,
  55.  
  56. #Personal stats
  57. :height => decimal_selection_array(5.0,7.0,0.1).to_a.sample,
  58. :body_type => (1..7).to_a.sample,
  59. :eye_colour => (1..6).to_a.sample,
  60. :drugs => (1..4).to_a.sample,
  61. :alcohol => (1..4).to_a.sample,
  62. :cigarettes => (1..3).to_a.sample,
  63. :likes => Faker::Lorem.sentence,
  64. :dislikes => Faker::Lorem.sentence,
  65. :bad_habits => Faker::Lorem.sentence,
  66.  
  67. #Favourite things
  68. :food => Faker::Lorem.sentence,
  69. :music => Faker::Lorem.sentence,
  70. :television => Faker::Lorem.sentence,
  71. :book => Faker::Lorem.sentence,
  72. :animal => Faker::Lorem.sentence,
  73. :place => Faker::Lorem.sentence,
  74. :possesion => Faker::Lorem.sentence
  75.  
  76.  
  77. )
  78.  
  79.  
  80.  
  81.  
  82.  
  83. end
  84. end
  85. end
  86.  
  87. before_safe :build_profile
  88.  
  89. # before running this task comment out: before_create :build_profile in user.rb in order to make this work correctly
  90.  
  91. namespace :db do
  92. desc "Create user records in the development database."
  93. task :populate => :environment do
  94. require 'faker'
  95.  
  96.  
  97. def randomDate
  98. "#{(1900..2012).to_a.sample}-#{(1..12).to_a.sample}-#{(1..28).to_a.sample}"
  99. end
  100.  
  101. def decimal_selection_array(start,limit,step_size=1)
  102. decimal_array = (start..limit).step(step_size).map{|i| i.to_s}.to_a
  103. decimal_array.insert(0,"Below #{start.to_f}")
  104. decimal_array.insert(-1,"Above #{limit.to_f}")
  105. end
  106.  
  107. 1000.times do |n|
  108. username = "#{Faker::Name.first_name}#{n+1}"
  109. User.create!(
  110. :username => username.gsub(/[^0-9a-z]/i, ''),
  111. :email => Faker::Internet.email,
  112. :password => "foobar"
  113. )
  114.  
  115. Profile.create!(
  116. :user_id => "#{n+1}",
  117. :motd => Faker::Lorem.sentence,
  118.  
  119. #Profile details
  120. :first_name => Faker::Name.first_name.gsub(/[^a-z]/i, ''),
  121. :last_name => Faker::Name.last_name.gsub(/[^a-z]/i, ''),
  122. :birthday => randomDate,
  123. :gender => (1..2).to_a.sample,
  124. :marital_status => (1..7).to_a.sample,
  125. :sexual_preference => (1..3).to_a.sample,
  126. :ethnicity => (1..10).to_a.sample,
  127. :country => Faker::Address.country,
  128. :location => Faker::Address.country,
  129.  
  130. #About the user
  131. :about_me => Faker::Lorem.paragraph,
  132.  
  133. #Personal stats
  134. :height => decimal_selection_array(5.0,7.0,0.1).to_a.sample,
  135. :body_type => (1..7).to_a.sample,
  136. :hair => (1..7).to_a.sample,
  137. :eye_colour => (1..6).to_a.sample,
  138. :drugs => (1..4).to_a.sample,
  139. :alcohol => (1..4).to_a.sample,
  140. :cigarettes => (1..3).to_a.sample,
  141. :likes => Faker::Lorem.sentence,
  142. :dislikes => Faker::Lorem.sentence,
  143. :bad_habits => Faker::Lorem.sentence,
  144.  
  145. #Favourite things
  146. :food => Faker::Lorem.sentence,
  147. :sport => Faker::Lorem.sentence,
  148. :music => Faker::Lorem.sentence,
  149. :television => Faker::Lorem.sentence,
  150. :book => Faker::Lorem.sentence,
  151. :animal => Faker::Lorem.sentence,
  152. :place => Faker::Lorem.sentence,
  153. :possession => Faker::Lorem.sentence
  154.  
  155.  
  156. )
  157.  
  158.  
  159. end
  160. end
  161. end
Add Comment
Please, Sign In to add comment