2013年2月9日土曜日

rails: rake aborted! uninitialized constant Movie


I got error when I try to use rake within Rails3 environment. (assume that there is a model named "Blog")


$ cat lib/tasks/test.rake
desc "mytest"
task :mytest do
  p Blog
end

$ rake mytest
rake aborted!
uninitialized constant Blog


the problem, was that I lack of ":environment" like bellow.


- task :mytest do
+ task :mytest => :environment do


I don't know why.. but anyway it works ^^;

2013年2月5日火曜日

moving gitolite server

old server



# backup repositories somehow.
user$ tar czvf repo.tgz /path/to/gitolite/repositories/


new server


user$ sudo yum install gitolite
user$ rpm -qa | grep gitolite
gitolite-2.3.1-1.el5

user$ sudo su -l gitolite


# initialize gitolite with dummy pub key
gitolite$ ssh-keygen -q -N '' -f dummy
gitolite$ gl-setup dummy.pub
creating gitolite-admin...
Initialized empty Git repository in /var/lib/gitolite/repositories/gitolite-admin.git/
creating testing...
Initialized empty Git repository in /var/lib/gitolite/repositories/testing.git/
[master (root-commit) a523b5b] gl-setup dummy.pub
 2 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100644 conf/gitolite.conf
 create mode 100644 keydir/dummy.pub


# change it same as old server if you neeed
gitolite$ vim .gitolite.rc


# swap repositories directory with backup
gitolite$ rm -rf ./repositories

gitolite$ cd /path/to/backup/
gitolite$ tar xzvf repo.tgz
gitolite$ mv repositories /var/lig/gitolite/


# overwrite repository by empty commiting
gitolite$ cd /tmp
gitolite$ git clone /var/lib/gitolite/repositories/gitolite-admin.git
Cloning into gitolite-admin...
done.

gitolite$ cd gitolite-admin
gitolite$ git commit --allow-empty -m 'move gitolite server'
[master 1656534] move gitolite server

gitolite$ gl-admin-push -f
Counting objects: 1, done.
Unpacking objects: 100% (1/1), done.
Writing objects: 100% (1/1), 190 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To /var/lib/gitolite/repositories/gitolite-admin.git
   d36c1e8..1656534  master -> master

gitolite$ exit


# confirm
user$ git clone ssh://gitolite@localhost/gitolite-admin
Cloning into gitolite-admin...
remote: Counting objects: 62, done.
remote: Compressing objects: 100% (54/54), done.
Receiving objects: 100% (62/62), 8.95 KiB, done.
remote: Total 62 (delta 12), reused 0 (delta 0)
Resolving deltas: 100% (12/12), done.


done! :)

Thanks

http://sitaramc.github.com/gitolite/rare.html#existing

http://stackoverflow.com/questions/9835235/moving-gitolite-server

2013年2月2日土曜日

mongoid on rails: diff with Railscast tutorial for mongoid >= 3.0.0

Problem

there is a mongoid great tutorial:
http://railscasts.com/episodes/238-mongoid

but this tutorial doesn't work for mongoid >= 3.0.0.


you might see some error messages like:

undefined method `key?' for nil:NilClass

undefined method `referenced_in' for Article:Class

undefined method `references_many' for Author:Class

undefined method `article_comments_path' for

No route matches [POST] "/articles/.../comments"


it seems that there was some changes its specification.




Solution

all you need to modify for suits 3.0.0 are bellow:

diff --git a/app/models/article.rb b/app/models/article.rb
index a00e637..386536a 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -5,5 +5,5 @@ class Article
   field :published_on, :type => Date
   validates_presence_of :name
   embeds_many :comments
-  referenced_in :author
+  belongs_to :author
 end
diff --git a/app/models/author.rb b/app/models/author.rb
index 7741009..e52989e 100644
--- a/app/models/author.rb
+++ b/app/models/author.rb
@@ -1,6 +1,6 @@
 class Author
   include Mongoid::Document
   field :name, type: String
-  key :name
-  references_many :articles
+  field :_id, type: String, default: ->{ name }
+  has_many :articles
 end
diff --git a/config/routes.rb b/config/routes.rb
index 31cc8de..45ac1c4 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -2,7 +2,9 @@ Mongoid01::Application.routes.draw do
   resources :authors
 
 
-  resources :articles
+  resources :articles do
+    resources :comments
+  end
 
   # The priority is based upon order of creation:
@@ -54,7 +56,7 @@ Mongoid01::Application.routes.draw do
 
   # You can have the root of your site routed with "root"
   # just remember to delete public/index.html.
-  # root :to => 'welcome#index'
+  root :to => 'articles#index'
 
   # See how all your routes lay out with "rake routes"

happy coding ! :)

mongoDB backup and restore

Backup



-[28701]% mongodump --version

mongodump version 2.0.3

-[28680]% mongodump -d test -o /tmp/mongobackup/

connected to: 127.0.0.1
DATABASE: test   to     /tmp/mongobackup/test
        test.iwa to /tmp/mongobackup/test/masaki925.bson
                 1 objects
        test.system.indexes to /tmp/mongobackup/test/system.indexes.bson
                 3 objects

-[28699]% tree /tmp/mongobackup/

/tmp/mongobackup/
└── test
    ├── masaki925.bson
    └── system.indexes.bson

Restore



-[28702]% mongorestore --version

mongorestore version 2.0.3

-[28700]% mongorestore -d test_restore /tmp/mongobackup/test/

connected to: 127.0.0.1
Sat Feb  2 10:57:44 /tmp/mongobackup/test/masaki925.bson
Sat Feb  2 10:57:44      going into namespace [test_restore.masaki925]
1 objects found
Sat Feb  2 10:57:44 /tmp/mongobackup/test/system.indexes.bson
Sat Feb  2 10:57:44      going into namespace [test_restore.system.indexes]
Sat Feb  2 10:57:44 { key: { _id: 1 }, ns: "test_restore.masaki925", name: "_id_" }
1 objects found

More Info

Backup Strategies for MongoDB Systems — MongoDB Manual
http://docs.mongodb.org/manual/administration/backups/