A life committed to learning.

Category: Software

Getting Rails 3.0.3 running with Mysql

Just as a memory aid for me and though I can share this.

While experimenting the Ruby on Rails 3.0.3, I created an application that by default uses SQLite, but I want to use MySql instead.

So here the steps I gone through:

1) Install MySql Server

sudo apt-get install mysql-server

2) Install MySql client dev package

sudo apt-get install libmysqlclient-dev

3) Create a new mysql user to use during my development and tests
3.1 ) Connect to mysql

mysql -u root -p

3.2 Create a user

create user 'railsuser'@'localhost' identified by 'somepass';

3.3) Grant all privileges to the user, so rails can do everything for me, such as creating the databases:

grant all privileges on *.* to 'railsuser'@'localhost';

4) Edit my app Gemfile:
Comment, or remove, the following line

gem 'sqlite3-ruby', :require => 'sqlite3'

Add this line:

gem 'mysql2'

5) Run the Bundler tool, the dependency management tool for Rails app:

sudo bundle install

Hopefully, everything went just fine till here.

Now, just configure my database.yml to use MySql as a database:

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: railsuser
password: somepass
socket: /var/run/mysqld/mysqld.sock

test:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_test
pool: 5
username: railsuser
password: railsuser1234
socket: /var/run/mysqld/mysqld.sock

For production, just use the MySql user that I want in production.

6) Let rake create my databases, by running:

rake db:create

It’s done.

Continue Reading

Yeahh, I’m building it with enterprise ready technologies… who cares?

If you have an web product idea and want to test it, i.e. put it in front of your users, what it’s the most critical aspect to consider first? I usually get my head into a conflict, because I’m used to work with the so-called “enterprise ready technologies”, namely JEE. My mind if formatted to think about all aspects that a good enterprise product should have. I start giving more attention to a set of quality attributes like performance, usability, reusability, testability, portability, modifiability, etc, etc, and give less attention to one critical aspect, if I want to get a product in front of the users fast: productivity.

So, I start looking to what is out there that is enterprise ready, like EJBs, JSF, Hibernate, JBoss, Tomcat, whatever, and start getting the pieces together…. out there in the world there was some a guy with the same product idea, but considered first the productivity aspect of the equation and started to materialize the idea with, let’s say, Ruby on Rails…. guess who’s the winner?

I’m not against the use of JEE, of course, I just think that in some cases it does not make sense. I also have this false argument in my mind: What if I start building it with PHP and then hit a performance problem with my 1Million users? Well, If I ever had one million users, I’ll be happy to deal with these performance issues 🙂

Continue Reading

Where’s my Java reflection?

Some people like it, others don’t. I like it, it’s so cool to make generic code, but as I ‘ve been experimenting with GTW to know it’s capabilities I found that GWT does not support Reflection. It uses instead a mechanism called Deffered Binding to overcome the lack of reflection.

When doing some kind of framework code with GWT, soon you’ll realize that you need reflection, as I did. Bad luck, you’ll have to dig into the underground features of GWT, namely deferred bindings and generators. You can learn the theoretical traits of Deferred Bindings in the documentation: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html

Let me explain my scenario.

Continue Reading

Expressive programming in Java

In Ruby On Rails, for example, you can use a construct like this:

raise ActiveRecord::RecordNotFound unless condition

It’s simple to understand and easier to use, though you can also use the more classical format

if !condition
    raise ActiveRecord::RecordNotFound
end

Because of that I like also to use this kind of expressiveness in Java. Instead of thinking like:

if this then do that

I like to think like:

do that if this

Of course, is a matter of felling well while programming 🙂

Let’s take an example in Java. In this example there’s a simple API to put something in a cache and you will use a “key” to identify your objects in the cache. But because I, as API designer, don’t want you to use the string “key” as key, I will code the cache like this:

package eu.jpereira.exprogramming.cache;
public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    if ( key.equals("key") ) {
      throw new Exception("Hey dude, key cannot be 'key'!!");
    }
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }	
}

But I prefer doing something like this:

package eu.jpereira.exprogramming.cache;
import static eu.jpereira.exprogramming.cache.ExceptionRaiser.*;

public class MyCache {

  public static void addToCache(String key, Object object) throws Exception {
    throwIfTrue(new Exception("Hey dude, key cannot be 'key'!!"),key.equals("key"),);
    //do that cache thing
    System.out.println("Cool! You've added it to the cache!!");
  }	
}

And the throwIfTrue method is coded like this:

package eu.jpereira.exprogramming.cache;

public class ExceptionRaiser {

  public static <T extends Throwable> void throwIfTrue(T exception, boolean condition) throws T {
    if (condition) {
      throw exception;
    }
  }
}

Even If at the first sight it may seems to be a lot of more work, it’ll pay off with my amusement while programming 🙂 Moreover, it’s easier to understand and to program like this.

Continue Reading

Programmer maturity

You’re a mature programmer when you’re willing to accept critics about your code.

You smell a critic. Ohh it’s about your code… There is a fire inside you, burning your pride. You can’t avoid expressing that anger through your actions. You can’t avoid showing me that lack of confidence on your work.
— from “The immature programmer”

Continue Reading

One line of code paranoia

If you have some java method like this:

public void doSomething(String oneArg, Object otherArg, Map<String, Object> mapArgs);

You can use your method somewhere in you code like this:

Map<String, Object> params = new HashMap<String, Object>();
params.add("key1", "Value1");
params.add("key1", "Value1");
doSomething("str1", someInstance, params);

Or if you’re paranoid about the number of lines of code you write, you can do something like:

doSomething("str1", someInstance, new HashMap<String, Object>(){{put("key1", "Value1");put("key1", "Value1");}});

Basically, you’re overriding the new anonymous HashMap and adding it a new initialization block…

Continue Reading
Continue Reading

Set focus on first input of a form

Isn’t annoying when you open a web page with a form and then have to click on the first input to start entering our data?

Well, it’s so easy to make this automatically with Javascript and JQuery

$("input:visible:enabled:first").focus();
Continue Reading

Look for the obvious first…

As I like to experiment cool web development frameworks, I decided to make a quick tour on Play! Framework. It was queued 🙂

So, here I was trying the persistence from the framework, that is basically JPA 2.0 with Hibernate as the persistence provider. I was also very interested in knowing what the framework could provide me for data validation, you know, that basic stuff that a framework should give us for free before it can be called wed framework.

I started with a very simple JPA entity like this one:

Continue Reading
Continue Reading