Category: Rails

Learning Mongoid – Build scalable, efficient Rails web applications with Mongoid – Book review

First of all I will point one thing: I'm not a professional book reviewer. I don't do this too often. Probably because I don't have enough time. However, I've decided to do a review of "Learning Mongoid" because I wanted to learn something new and Packt Publishing was kind enough to lend me a copy for this review. So here it is. I'll start with things that I really liked. As usual, there were some things that could be corrected, but if you have Rails experience, this book will be really helpful for you.

7501OS_Learning Mongoid

Things I did like about this book

It's not extremely long

You may consider this an issue, but I've found this really helpful. Chapters aren't long, so getting through them is not painful. I bet you've sometimes wondered "what is the author getting at?". Not with this one. Chapters (and the book itself) are really consistent. You won't get bored reading this one or feel like giving up.

A lot of examples

I don't like theoretical texts and books, without any examples of good practices. We're developers, we should be able to play around with new stuff that we learn! And one of the things that I really liked about Learning Mongoid is that I was able to copy-paste almost every example and play-around with it on my computer.

Field aliases

Even now I can recall times, where I had to rename fields, so I would be able to create an index for them :). I don't know why, but this is not a thing that is covered in tutorials or other books (at least not in those that I know). On the other hand this is super useful. I was really surprised to see this one here. It made me realize one thing - this book was written by other guys who develop Rails-Mongoid software.

Geospatial searches and querying in general

When doing a lot of Geolocalization stuff - Mongo can be really helpful and can simplify a lot of things. All basic geo-search options are covered in this book. In general, the whole querying chapter is well-written and together with aggregation framework, it covers all common cases that you may want to use.

Performance tuning and maintenance

Performance is really important. If you don't do it right, you might end up with really slow application. This book covers the basics of both - performance tuning and Mongoid maintenance, so after reading it you will be able to use some of Mongo and Mongoid properties to gain few seconds of users life ;)

Things I didn't like about this book

A good book - but not sure whether or not for pros or beginners

Learning Mongoid by Packt Publishing is a solid book about Mongoid, although it lacks some information that would be super useful for beginners. I've got a feeling that it covers most of "stuff you need to know to start working with Mongo and Mongoid", but as mentioned above, when it comes to people who want to start using Mongoid and they know only a bit about Ruby - it can be harsh.

Install RVM - but do this on your own

I know that this book should be (and it is!) about Mongoid, but since we're talking about it, it is worth at least mentioning how to install RVM, especially because it is one of the prerequisites. 1-2 pages about RVM would be really helpful.

Need some config hints? Well not this time

The second thing that is lacking is a Mongoid setup instruction. Not even a word on what should/should not be in mongoid.yml, what are the most important options, etc. There is even mention of it in the book:

There are entirely new options in mongoid.yml for database configuration

Although none of the changes are listed. No information about replica_set, allow_dynamic_fields, preload_models or any other important setup options. This is a must be in any good Mongoid book.

Want to upgrade to most recent Mongoid version? We won't help you out :(

I've mentioned that below, but I will point it out again. Authors say, that there are several differences between new and old Mongoid, although they don't list them (except IdentityMap). I think they should.

Want to migrate your app to Mongoid?

Maybe you want to move your app from ActiveRecord to Mongoid (I did it few times myself)? If so, "Learning Mongoid" will help you handle Mongo part, but it won't help you with the migration process itself. Sodibee (example book app) is a Mongoid base app. Maybe authors assumed, that if you master ActiveRecord and Mongoid, you don't need any extra help to switch between them...

Summary

Would I recommend this book? Yes - I already have! It can be a solid Mongo and Mongoid starting point for begginers (apart some issues that I've mentioned) and a "knowledge refresher" for people that use Mongoid longer that few weeks. It is well written and it has a lot of examples. Really a good one about Mongoid.

If you're interested in buying this book, you can get it here.

Puma Jungle script fully working with RVM and Pumactl

Finally I've decided to switch some of my services from Apache+Passenger to Nginx+Puma. Passenger was very convenient when having more than one app per server. Although I used Passenger Standard edition and sometimes apps that should have max 1-2 workers would consume at least half of pool. I was not able to prioritize apps easily. Also it started to get pretty heavy and disadvantages finally exceeded benefits of using it.

Switching static and PHP-based content from Apache to Nginx was really simple. I've installed Nginx, started it on port 82 and 445 for HTTPS and to maintain uptime, I just proxy passed each app at a time from Apache to Nginx. That way I kept Apache temporarily as a proxy engine for all the "simple to move" content and as a Passenger wrapper for my Rails apps.

I've decided to use Puma as my default Rails server for apps on this particular machine. Everything worked great until I've tried to use Jungle script to manage all the apps at once (and add it to init.d). After few seconds of googling I found Johannes Opper post on how to configure Puma Jungle to work with RVM. It turned out, you just need to edit /usr/local/bin/run-puma file and add this line:

# Use bash_profile of your rvm/deploy user
source ~/.bash_profile

After that I was able to start all the apps at once:

/etc/init.d/puma start

Unfortunately, I was not able to stop/restart Puma instances with this script. It was giving me this message:

# when stopping
/etc/init.d/puma: line 99: pumactl command not found
# when restarting
/etc/init.d/puma: line 129: pumactl command not found

Since there's an RVM on the server, Puma was installed as one of gems for one Ruby version. The same goes for Pumactl. To be able to use Pumactl I had to change the /etc/init.d/puma script a bit. First I had to change it from sh to bash script:

#! /bin/bash
# instead of
#! /bin/sh

After that I had to add my deploy user bash profile:

source ~/.bash_profile

and change how pumactl is executed in few places:

do_stop_one method (line 99):

# replace this
pumactl --state $STATEFILE stop
# with this
user=`ls -l $PIDFILE | awk '{print $3}'`
su - $user -c "cd $dir && bundle exec pumactl --state $STATEFILE stop"

do_restart_one (line 129):

# replace this
pumactl --state $dir/tmp/puma/state restart
# with this
user=`ls -l $PIDFILE | awk '{print $3}'`
su - $user -c "cd $dir && bundle exec pumactl --state $dir/tmp/puma/state restart"

do_status_one (line 168):

# replace this
pumactl --state $dir/tmp/puma/state stats
# with this
user=`ls -l $PIDFILE | awk '{print $3}'`
su - $user -c "cd $dir && bundle exec pumactl --state $dir/tmp/puma/state stats"

and that's all. After that you should be able to manage all your Puma apps with Jungle.

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑