Thursday, February 26, 2009

Errno::ECONNREFUSED: Connection refused - connect(2)

Errno::ECONNREFUSED: Connection refused - connect(2)

If you get this error, one of the reason could be incorrect or no ActionMailer settings. The following structure needs to be present in your production.rb (or whatever config file):

ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'mydomain.com',
:authentication => :plain,
:user_name => 'user@mydomain.com',
:password => 'password'
}

Tuesday, February 17, 2009

Using define_method to create a method that takes arguments

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/211436
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/211440

Example:

Setting.all.each do |setting|
define_method setting.name do |*a|
r = find(:first, :include => :setting, :conditions => ["setting_id = ?", setting.id])
full_record = (a.length == 0) ? false : a[0]
full_record ? r : r.value
end
end

Wednesday, January 28, 2009

after_initialize and after_find can't be private

after_initialize and after_find can't be private. Another note: they need to be explicitly define like:

def after_initialize
# do stuff here.
end

Sunday, December 28, 2008

Current option not getting selected in select box?

I have a f.select(:attr1, options) in one of my form. But after I submit and if there are any errors in the form it should show the errors and retain all the values in the form. But this attr1 was loosing its value. Instead of the previously selected option, it being reset to the first option. What was wrong?

att_accessible! The attribute wanted to be selected automatically must be in the attr_accessible list. Otherwise Rails won't attach "selected" property to that option.

Thursday, November 20, 2008

Installing sqlite for rails development

  1. sudo apt-get install sqlite3
  2. sudo gem install sqlite3-ruby
  3. rails my_project
  4. rake db:create

Wednesday, November 19, 2008

Duplicating tables and problem with the structure

To duplicate a table:
mysql> CREATE TABLE products SELECT * FROM old_products;

But the problem with above statement is structure of products may not be the same as old_products. Attribute properties are not copied to the new table. For example of you have an auto_increment field named id, the auto_increment property won't be copied to products table. products.id will be a normal integer field with the default value set to 0. It also means the new id field won't be the primary field. Took me 3 hours to figure this out.

So the best way to duplicate a table is:
1. CREATE TABLE products LIKE old_products;
2. INSERT INTO products SELECT * FROM old_products;

Thursday, October 30, 2008

Common MySQL stuff

To modify an existing column definition:

ALTER TABLE applications modify id INTEGER NOT NULL AUTO_INCREMENT FIRST;

To rename a column:

ALTER TABLE applications CHANGE old_col new_col;