Yeah we can implement using "ACTIVERECORD-IMPORT" library, in rails 3.x (It will not support for rails 2.x versions).
Just look at the following code snippets
Note: This library only will work on Rails 3.x versions only not 2.x versions, so keep it this point before your implementation.
Just look at the following code snippets
Class User < ActiveRecord::Base
######## schema information about user ##########
login
user_name
email
address
city
state
country
zipcode
######## schema information ended here #########
has_many :posts
has_many :comments
end
Suppose if you want to store/save 5-10 users into database table, normally you can do the following way######## schema information about user ##########
login
user_name
address
city
state
country
zipcode
######## schema information ended here #########
has_many :posts
has_many :comments
end
test_name= "user_name"
10.each do |number|
User.create!(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
The above code works fine without any issues. Just think about if you want to save 1000 or 2000 records, that time the above code snippets will eat huge time to save the records. So avoiding/handle that huge time difference Ruby on Rails, We can use the library called "activerecord-import".10.each do |number|
User.create!(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
Now we can insert 1000/2000 records at one shot using activerecord-import library. Here is the small code snippet implementation for your reference
users = []
test_name= "user_name"
1000.each do |number|
users << User.new(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
User.import users
Thats it. In single shot your entire 1000 records inserted into your database.users = []
test_name= "user_name"
1000.each do |number|
users << User.new(:user_name => test_name + number,:login_name => "login"+number,..etc fields)
end
User.import users
Note: This library only will work on Rails 3.x versions only not 2.x versions, so keep it this point before your implementation.
It was really a nice article and i was really impressed by reading this Ruby on Rails Online Training
ReplyDelete