Archiwum

Posty oznaczone ‘Rails’

Rails – JQuery and link_to with delete method

Sierpień 28th, 2011 Brak komentarzy

If you use JQuery and experience problems with links using DELETE method then use jquery-ujs gem or just download modified rails.js script.

Tagi:,

Rails – How to rollback database migration

Sierpień 28th, 2011 Brak komentarzy

To rollback last migration use command:

$ rake db:rollback
==  AddAccessedAtToAssets: reverting ==========================================
-- remove_column(:assets, :accessed_at)
   -> 0.2357s
==  AddAccessedAtToAssets: reverted (0.2359s) =================================

To rollback (for example) last three migrations use STEP parameter:

$ rake db:rollback STEP=3
==  AddFolderIdToAssets: reverting ============================================
-- remove_column(:assets, :folder_id)
   -> 0.2230s
==  AddFolderIdToAssets: reverted (0.2232s) ===================================
 
==  CreateFolders: reverting ==================================================
-- drop_table(:folders)
   -> 0.0723s
==  CreateFolders: reverted (0.0724s) =========================================
 
==  AddAttachmentUploadedFileToAsset: reverting ===============================
-- remove_column(:assets, :uploaded_file_file_name)
   -> 0.2546s
-- remove_column(:assets, :uploaded_file_content_type)
   -> 0.2616s
-- remove_column(:assets, :uploaded_file_file_size)
   -> 0.2263s
-- remove_column(:assets, :uploaded_file_updated_at)
   -> 0.2342s
==  AddAttachmentUploadedFileToAsset: reverted (0.9774s) ======================
Tagi:

Rails/Ubuntu – Cannot build mysql gem?

Maj 18th, 2011 Brak komentarzy

If you see „Failed to build gem native extension” error when trying to install mysql gem then you need to install libmysql++-dev package.

$ sudo gem install mysql
[sudo] password for milosz: 
Fetching: mysql-2.8.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
	ERROR: Failed to build gem native extension.
[...]
$ sudo apt-get install libmysql++-dev
$ sudo gem install mysql
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...
Tagi:, ,

Rails – Quick migration example

Maj 14th, 2011 Brak komentarzy

Let’s say that we want to add friendly_name field to bookmark_categories table. Of course this field cannot be null and needs to contain specific values.

So we create new migration:

$  rails generate migration add_friendly_url_to_bookmark_categories

Next we need to edit migration file (just look at comments):

class AddFriendlyUrlToBookmarkCategories < ActiveRecord::Migration
  def self.up
    # add friendly_name field to bookmark_categories table
    # we cannot forbid null yet
    add_column :bookmark_categories, :friendly_name, :string, :limit => '32', :unique => true
 
    # update friendly_name for each category
    BookmarkCategory.find(:all).each do |category|
     category.friendly_name = category.name.parameterize
     category.save
    end
 
    # finally forbid null values
    change_column :bookmark_categories, :friendly_name, :string, :limit => '32', :null => false, :unique => true
  end
 
  def self.down
    # just remove this column on downgrade
    remove_column :bookmark_categories, :friendly_name
  end
end

Execute migration:

$ rake db:migrate
(in /home/milosz/pim)
==  AddFriendlyUrlToBookmarkCategories: migrating =============================
-- add_column(:bookmark_categories, :friendly_name, :string, {:limit=>"32", :unique=>true})
   -> 0.0029s
-- change_column(:bookmark_categories, :friendly_name, :string, {:limit=>"32", :null=>false, :unique=>true})
   -> 0.1297s
==  AddFriendlyUrlToBookmarkCategories: migrated (1.1918s) ====================

Rails3 – How to run only specific tests?

Maj 8th, 2011 Brak komentarzy

To run all tests:

$ rake test

To run only unit tests:

$ rake test:units

To run only functional tests:

$ rake test:functionals

To run only integration tests:

$ rake test:integration

To run only tests in specific file:

$ ruby -Ilib:test test/unit/user_test.rb

Run only specic test:

$ ruby -Ilib:test test/unit/user_test.rb -n test_should_generate_salt

Run only couple of specic tests:

$ ruby -Ilib:test test/unit/user_test.rb -n /.*password.*/

To see executed methods use verbose mode:

$ ruby -Ilib:test test/unit/user_test.rb -n /.*user.*/ -v
Loaded suite test/unit/user_test
Started
UserTest#test_should_validate_username_length: 0.82 s: .
UserTest#test_should_validate_username_uniqueness: 0.04 s: .
 
Finished in 0.886984 seconds.
 
2 tests, 4 assertions, 0 failures, 0 errors, 0 skips
 
Test run options: --seed 64903 --verbose --name "/.*user.*/"
Tagi: