0

I have a Ruby on Rails application (ruby 1.8.7 ; rails 2.3.18). I have a customised rake task (basically a ruby program) which compares two arrays, and then stores their difference in a third array (arr3 = arr1 - arr2). In the application there is a user model. And I have a database script that suspends (deletes) users from the model/db. So I want to call this database script from the rake task after the arr3 is created. I want to run this script until all the elements in arr3 are deleted. The arrays give the difference between two lists of users.

Can anyone guide me on this? How can I call a database script from a rake task? Anything on this will be of great help.

The rake file is as below:

namespace :db do
arr1 = Array.new 
arr2 = Array.new 
arr3 = Array.new

desc "load data from csv"
task :load_is_data do
  require 'fastercsv'
  CSV.foreach("C:/Sites/rails_project/demo_app/list1.csv") do |row|
arr1 << row[0]
  end

 CSV.foreach("C:/Sites/rails_project/demo_app/list2.csv") do |row|
arr2 << row[0]
  end

  arr3 = arr1-arr2 
  puts "Inactive: #{arr3}"

 #CODE TO BE ADDED, TO CALL DATABASE-SCRIPT

 end
end

Thanks, Bhargav

buruzaemon
  • 3,847
  • 1
  • 23
  • 44
bhargav
  • 267
  • 3
  • 8
  • 17
  • Maybe you could have a look at this similar question: http://stackoverflow.com/questions/16074630/how-can-i-remove-a-column-from-table-using-rails-console – Jun Zhou Apr 26 '13 at 07:14
  • I do not want to remove an entire column from the database. Just want to delete some entries by using the db script. – bhargav Apr 26 '13 at 07:42
  • Why do you need to call a database script? Are you not using ActiveRecord? – Yule Apr 26 '13 at 09:07

1 Answers1

0

you can use any below gems.

gem "populator", "~> 1.0.0"
gem 'faker'
gem "random_data", "~> 1.6.0"

Code will be something like this.

admin = AdminUser.create :email => 'xxxxxx@gmail.com', :password => 'xxxx1234', :password_confirmation => 'xxxx1234', :role => 'SuperAdmin', :name => 'SuperAdmin'
  puts 'SuperAdmin created: ' << admin.email



      10.times do |index|
        User.create! :fname => Random.firstname,
          :lname => Random.lastname,
          :email => Random.email,
          :password => 'dcs1234',
          :password_confirmation => 'dcs1234'
      end
Pravin Mishra
  • 8,298
  • 4
  • 36
  • 49
  • Hi Pravin, I am sorry. I could not understand your soln. I mean what will these gems do ? Also why i need to create a superuser or a superadmin ? Is there a thread/link where i can look for better understanding of your concept ? I basically wanted to know how i can call a database script (existing) from a rake task, so that it can iterate through the elements of the array and perform the desired action. Thanks – bhargav Apr 26 '13 at 09:06