by sandipransing
Hi I'm Sandip Ransing, a ruby on rails developer in Pune (India).
I did my bachelor in Computer Engineering from PES Modern College of Engineering, Pune.
In July 2007, I started software development career in Java.
After few months passed, I heard about ruby language from my friend Haribhau Ingale and got attention towards ruby and rails and found much interesting language to learn and development.
I started learning ruby from Internet, Blogs, Ebooks.
I am really thankful to Josh Software Private Limited who gave me opportunity to learn and work in ruby on rails (ROR).
I am working with Josh Software private Limited from February 2009. Josh is one of best ruby on rails development company who follows methodologies of software development.
It's always fun for me to work in ruby on rails. I always strive to keep myself updated with latest.
Thurst of getting new is endless :) :)
To contact me, email san2821@gmail.com
Read More…
by sandipransing
Vim install On CentOS
yum install vim-enhanced
Vim install on Ubuntu machine
Install vim-full using command
apt-get install vim
While coding with ruby, html, erb, haml, js and stylesheets.
It is great pain to indent code. Using rails vim one can easily
keep code always indented.
This increases code readability and minimizes effort, bugs and
finally proves ease of using vim editor.
rails.vim script contains lot of syntax highlighter and indentation
plugins that really helps development needs.
If you have git installed then clone it under .vim directory of your profile
git clone git://github.com/sandipransing/rails_vim.git ~/.vim
To install from zip file download & extract it inside ~/.vim directory
download link: http://www.vim.org/scripts/download_script.php?src_id=11920
wget http://www.vim.org/scripts/download_script.php?src_id=11920 /rails.vim
This is how my editor looks like
Open your ~/.bashrc and at the bottom add:
alias vi=vim
export EDITOR=vim
Read More…
by sandipransing
Extend enumerable functionality to iterate along with index
module Enumerable
def collect_with_index(i=0)
collect{|elm| yield(elm, i+=1)}
end
alias map_with_index collect_with_index
end
Example use :
ree-1.8.7-2010.01 > ['ruby', 'rails', 'sandip'].map_with_index{ |w,i| [w, i] }
#=> [["ruby", 1], ["rails", 2], ["sandip", 3]]
ree-1.8.7-2010.01 > ['ruby', 'rails', 'sandip'].collect_with_index{ |w,i| [w, i] }
#=> [["ruby", 1], ["rails", 2], ["sandip", 3]]
#By default index starts from zero to specify custom index to start from,
#pass index to collect_with_index
ree-1.8.7-2010.01 > ['ruby', 'rails', 'sandip'].map_with_index(-1){ |w,i| [w, i] }
#=> [["ruby", 0], ["rails", 1], ["sandip", 2]]
ree-1.8.7-2010.01 > ['ruby', 'rails', 'sandip'].map_with_index(5){ |w,i| [w, i] }
#=> [["ruby", 6], ["rails", 7], ["sandip", 8]]
Read More…
by sandipransing
This error appears when same action gets called twice. There might be chances to have controller with same name twice inside app as well as plugins
In my case the error appeared while migrating from rails 2.1 to 2.3.
Application was having application.rb and application_controller.rb
Files deleted: trunk/app/controllers/application.rb
And that solved problem !
Read More…
by sandipransing
1. Ruby http://tryruby.org
2. Hpricot http://hpricot.com
3. Regular expressions http://rubular.com
4. Exceptions http://hoptoadapp.com/
5. Application Performance http://newrelic.com
6. Ruby Doc http://www.ruby-doc.org/
7. Rails API http://api.rubyonrails.org/
8. Ruby gems sources http://rubyforge.org / http://gems.github.com / http://gemcutter.org
9. Listing remote gems from source gem list --remote --source http://gems.github.com
10. List of Rails Plugins http://wiki.rubyonrails.org/rails/pages/Plugins
http://www.agilewebdevelopment.com/plugins
Read More…
by sandipransing
1. Copy One database to another database on same host
mysqldump -uroot -p | mysql -uroot
2. Copy database to another database on remote host
mysqldump -uroot -p | ssh host2 "mysql -uroot "
Read More…
by sandipransing
1. To create local variables in ruby dynamically.
Use eval method in ruby
ree-1.8.7-2010.01 > eval("local=4")
=> 4
ree-1.8.7-2010.01 > p local
4
ree-1.8.7-2010.01 > eval("local_#{1}=4")
=> 4
ree-1.8.7-2010.01 > puts local_1
4
2. To create & get instance variables dynamically in ruby.
instance_varaiable_set & instance_varaiable_get methods are provided and no need to do it using eval.
ree-1.8.7-2010.01 > (0..3).each do |i|
ree-1.8.7-2010.01 > instance_variable_set("@instance_#{i}", i*i+1)
ree-1.8.7-2010.01 ?> end
=> 0..3
ree-1.8.7-2010.01 > @instance_0
=> 1
ree-1.8.7-2010.01 > @instance_2
=> 5
3. Dynamic constant variable creation in ruby
ree-1.8.7-2010.01 > class Example
ree-1.8.7-2010.01 ?> end
=> nil
ree-1.8.7-2010.01 > Example.const_set('A',200)
=> 200
ree-1.8.7-2010.01 > Example::A
=> 200
Read More…
by sandipransing
Eval method in ruby executes string/expression passed as parameter.
Example:
irb > eval("5+3")
=> 8
irb > eval("a=5")
=> 5
irb > eval("b||=a")
=> 5
Its part of ruby meta-programming and not recommended approach unless there is no any alternative to do.
Read More…
by sandipransing
Example of polymorphic association using has_many, through, source.
Here post is polymorphic resource which belongs to the author [can be student, teacher]
# app/models/student.rb
class Student < ActiveRecord::Base
has_many :posts, :as => author
end
# app/models/teacher.rb
class Teacher < ActiveRecord::Base
has_many :posts, :as => author
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs to :author, :polymorphic => true
end
Has_many through association on polymorphic models
# app/models/student.rb
class Student < ActiveRecord::Base
belongs_to :division
has_many :posts, :as => author
end
# app/models/division.rb
class Division < ActiveRecord::Base
has_many :students
has_many :student_posts, :through => :students, :source => :posts
end
Examples
ruby script/console
div = Division.first
div.student_posts
Read More…
by sandipransing
#user nobody;
user www-data;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
passenger_root /var/lib/gems/1.8/gems/passenger-2.2.8;
passenger_ruby /usr/bin/ruby1.8;
passenger_max_pool_size 3;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
root /home/josh/current/public; # <--- be sure to point to 'public'!
passenger_enabled on;
passenger_use_global_queue on;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
Read More…
by sandipransing
ActiveRecord find with join options retrieves object as readonly
station = Station.find( :first, :joins => :call, :conditions => ["customer_id = ? and date(insurance_expiry_date) = ?", customer.id, insurance_expiry_date ] )
Readonly object cannot modified and hence below line raises "ActiveRecord::ReadOnlyRecord" error.
station.update_attributes({ :customer_id => 12 })
If you have to write on read only object then you can pass following option to find query
:readonly => false
Now below find is permitted to do write on fetched object records.
station = Station.find( :first, :joins => :call, :conditions => ["customer_id = ? and date(insurance_expiry_date) = ?", customer.id, insurance_expiry_date ], :readonly => false )
Read More…
|