Flowdock

Notes posted by ypetya

RSS feed
July 16, 2010
4 thanks

to set NULL => NO

use :null => false

change_column :my_table, :my_column, :integer, :default => 0, :null => false
July 16, 2010
2 thanks

Changing to MySql:BIGINT

I can change a column type from INT to BIGINT with this command:

change_column :my_table, :my_column, :bigint
June 23, 2010
0 thanks

Getting the client ip

From CgiRequest you can get the client ip in controller:

request.remote_ip
June 23, 2010
2 thanks

Accessing controller data

You can access controller attributes from views via the @controller variable.

It has some important attributes:

  • @controller.controller_name -> the name of the controller

  • @controller.request -> returns the ActionController::Request

    • @controller.request.method -> the request method ( get, post, put, delete )

    • @controller.request.host -> the request host ( ip address or hostname ) where your server runs

    • @controller.request.ip -> the ip where your browser runs

February 5, 2010
2 thanks

An alternate way to have a string ID as a primary key

You can disable automatically created primary key and add it to manually with mysql:

The migration file:

def self.up

  create_table( :my_special_table, :id => false ) do |t|
    t.string :id, :limit => 5, :null => :no
  end

  execute "ALTER TABLE my_special_table ADD PRIMARY KEY (id)"

end

Then in a before_save filter you can generate the primary key for yourself.

Use a transaction and be aware of uniqueness!

February 4, 2010 - (>= v2.1.0)
1 thank

All dates in the database are stored in UTC and all dates in Ruby are in a local timezone

With the timezone support introduced in Rails 2.1 the idea is that all dates in the database are stored in UTC and all dates in Ruby are in a local timezone.

Time.zone.now == Time.now # => false

as Peter Marklund lights up this in his blog:

http://marklunds.com/articles/one/402

“They will only be converted to UTC for you if they are ActiveSupport::TimeWithZone objects, not if they are Time objects. This means that you are fine if you use Time.zone.now, 1.days.ago, or Time.parse(”2008-12-23“).utc, but not if you use Time.now or Time.parse(”2008-12-23“)”

February 4, 2010
1 thank

Warning! Be aware of Active Record TimeZone

With the timezone support introduced in Rails 2.1 the idea is that all dates in the database are stored in UTC and all dates in Ruby are in a local timezone.

Time.zone.now == Time.now # => false

http://marklunds.com/articles/one/402

January 29, 2010
1 thank

Passing html options (Ruby hash parameters)

When you have two default hash parameters at the end of a function call, you need to use it as the following:

options = { :include_blank => true, :default => @my_object.my_method }

date_select :my_object, :my_method, options, :class => 'my_css_class'

You can try it for yourself on this example:

def test_funct a = {}, b = {}
  puts "a: #{a.inspect}"
  puts "b: #{b.inspect}"
end

test_funct :x => 'x', :y => 'y'  # all the parameters are collected for the hash a