Archive for the 'ruby' Category

26
Jun
12

change timezone without changing time


t = DateTime.now
t.change(:offset => "+0800")

will change the timezone to a +0800 one.

Advertisements
09
Jun
12

ruby, rvm and ubuntu

Here’s how to get a working installation of rvm on ubuntu:


sudo apt-get install curl git-core

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion libcurl4-gnutls-dev postgresql postgresql-contrib libpq-dev

curl -L get.rvm.io | bash -s stable --ruby

Next, add a user to postgresql:


sudo -u postgres createuser --superuser john
sudo -u postgres psql postgres
\password john
\q

And add this to your .bashrc:


[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm
30
May
12

rails translating attribute names

I found the answer to my question from here.
Here’s the way:


  activerecord:
    attributes:
      user:
        first_name: "Name first"

22
May
12

making sure your integration tests view the actual data in the db


Monkeypatch ActiveRecord with this:


class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

and your spec_helper with this:


RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end
end

Full info available on Nicholas Rutherford’s blog. Thanks Nicholas!

22
May
12

rails select only some columns


If you want to only select a few columns on a query, here’s how you can do it:

 YourModel.find(:all,:select => "column1,column2") 

23
Apr
12

access the database of a heroku application from another heroku application


First thing you have to do is find out the DATABASE_URL of the app you’re interested in. Run heroku config –app your_app to get it. Then, in the other app, run this:

heroku config:add DATABASE_URL=whatever_the_result_of_the_first_command_was

Establish an ActiveRecord connection:


  db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

  ActiveRecord::Base.establish_connection(
              :adapter  => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
              :host     => db.host,
              :username => db.user,
              :password => db.password,
              :database => db.path[1..-1],
              :encoding => 'utf8'
   )

02
Mar
12

cool ruby gem

state_machine is a nifty gem. Try it!




Blog Stats

  • 228,094 hits