Showing posts with label Rails. Show all posts
Showing posts with label Rails. Show all posts

Thursday, 14 February 2013

How to implement bulk insert in Rails

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

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

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".

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.

Note: This library only will work on Rails 3.x versions only not 2.x versions, so keep it this point before your implementation.


Saturday, 2 February 2013

How to install gems group specific in your rails application

In Ruby on Rails application Gemfile, while you defining gems we can make that gem in a particular group. For example some gems needs to be loaded only on test environment so on that time we can mention those kind of gems into one specified groups. so your gemfile should like this

gem 'aws-s3'
gem 'paperclip'
group :test do
  gem 'rspec'
  gem 'waitr'
  gem 'faker'
end

If you are planning to define just one gem into a particular group only you can use without blocks like this

gem 'rest-client', :group => :development
gem 'cucuber-rails', :groups => [:development,:test]  (cucuber-rails gems comes under both group)

Similarly you can make it :development,:default(group name) or give a meaningful names whatever you want as group name.

Finally you have to tell while running the "bundle install" command just ignore what are the groups to be ignored using --without.

bundle install --without development
bundle install --without development test (ignore both groups and install others)
The above command ignore whatever gems we mentioned in the development group. So it will install rest of the default group(if you are not mention any group  those gems comes under defaults group)gems and test group gems.

Note:
bundle install --without development (ignore development group gems)
bundle install (still bundle remembers --without development so result is still ignore development groups it will not install all gems)

bundle install --without nothing (just clearing cache, now all the gems to be loaded into the ruby loadpath) 


Monday, 21 January 2013

undefined method to_i in rails 3.2.11 upgrade

I created my rails application using Rails 3.2.3 version. After some period i come to know Rails 3.2.3 having some security issues, Here i have mentioned what is the issue

What is the exact security issue in Rails 3.2.3

People told that if we upgrade rails 3.2.3 into rails 3.2.11 issue will resolve otherwise we have to do some code tweaks for supporting rails 3.2.3 code base.

So we upgrade into rails 3.2.11. Now security issue was over But found some other issue called "undefined method to_i" error in few of the places. After search in Google, we come up with solutions

In rails 3.2.3
doctor = Doctor.first
patient = Patient.new
patient.doctor_id =doctor (Right side Object assignment is valid)
patient.save! (valid)

In rails 3.2.11
doctor = Doctor.first
patient = Patient.new
patient.doctor_id = doctor (Right side Object assignment is invalid)
patient.save! (invalid)
NoMethodError: undefined method 'to_i' error we got this kind of assignments.

patient.doctor = doctor (is valid both left and right side are object reference)


So conclusion is we must use wherever _id column definition have to use INTEGER type only not just Object assignments.