Posts Tagged ‘ruby’

Gotcha : Mysql::Error: Unknown database 'application_name_test' /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'

Posted in Uncategorized on July 19th, 2009 by ph-lee – Be the first to comment

After dealing with thisGotcha : Mysql::Error: Access denied for user ‘root’@’localhost’ you’ve hit another problem of…

This means rails is looking for the test database but can’t find it cause it’s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test so for example

my_app_test

Theres no need to copy/clone the database structure a across. Rails will replicate the development database as the test database.

Gotcha : Mysql::Error: Access denied for user 'root'@'localhost' (using password: NO) /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'

Posted in Uncategorized on July 19th, 2009 by ph-lee – 1 Comment

So you decided to run some tests on your new rails project and this error crops up…

Looks like you forget to configure the test database parameters in the configuration/database.yml file.

(Ruby on rails) Adding an admin area to your web application

Posted in Uncategorized on July 18th, 2009 by ph-lee – Be the first to comment

Most web applications have an admin area and a user area. Blogs, webstores, social sites pretty much any dynamic site. You’ll want an admin area to do all the backend stuff to manage all your content and users. [icebergist] gives one way of adding an admin area to your web app.

I’ve tried this method and so far it seems nice. It completely separates the two area giving you full control over each.

How to : Exceptions in ruby (on rails) coming from a Java or C# .NET background

Posted in Uncategorized on July 14th, 2009 by ph-lee – Be the first to comment

Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.

C# .NET Example
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// <summary>
/// constructor for creating a new user
/// </summary>
/// <param name="str_username">username of the user</param>
 
public User(string str_username)
{
   //check method parameters/arguments for null or empty
 
   if (String.IsNullOrEmpty(str_username))
   {
      //throw an exception since parameters entered are not valid
      throw new ArgumentException("argument str_username was null or empty", "str_username");
   }
 
   this.str_username = str_username;
}
 
/// <summary>
/// absurd method for the purpose of this example
/// creates two users
/// </summary>
 
public static void createBobAndEmpty()
{
   //try and create bob
 
   try
   {
      new User("Bob");
   }
   catch (ArgumentException e)
   {
      //this exception will not be thrown in this case
   }
 
   //try and create empty
 
   try
   {
      new User("");
   }
   catch (ArgumentException e)
   {
      //this exception will be thrown in this case
 
      //e is the variable storing the exception which we can print the exception message, variable of concern
      //in other cases the entire stack trace can be printed for debugging purposes
 
      //in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username
   }
   finally
   {
      //this finally block is optional and normally used for garbage collection/closing connections/closing files etc
   }
}

Java Example
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * constructor for creating a new User object
 *
 * @param str_username the username of the user
 */
 
public User(String str_username)
{
   //check parameter is not null
 
   if(str_username == null)
   {
      throw new NullPointerException("str_username is null");
   }
 
   //check parameter is not an empty string
 
   if(str_username.isEmpty())
   {
      throw new IllegalArgumentException("str_username is empty");
   }
 
   this.str_username = str_username;
}
 
/**
 * absurd method for the purpose of this example
 * creates 2 users
 */
 
public static void createBobAndEmpty()
{
   //try and create the user bob
 
   try
   {
      new User("Bob");
   }
   catch (NullPointerException e)
   {
      //this exception will not be thrown in this case
   }
   catch (IllegalArgumentException e)
   {
      //this exception will not be thrown in this case
   }
 
   //try and create user empty
 
   try
   {
      new User("");
   }
   catch (NullPointerException e)
   {
      //this exception will not be thrown in this case
   }
   catch (IllegalArgumentException e)
   {
      //this exception will be thrown in this case
 
      //e is the variable storing the exception which we can print the exception message, variable of concern
      //in other cases the entire stack trace can be printed for debugging purposes
 
      //in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username
   }
   finally
   {
      //this finally block is optional and normally used for garbage collection/closing connections/closing files etc
   }
}

Ruby

Firstly Ruby uses different terms/keywords compared to Java or C#, the usual try/catch/throw/finally are different. Instead…

  • “try” becomes “begin”
  • “catch” becomes “rescue”
  • “finally” becomes “ensure”

So in Ruby
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#class constructor for a user
#username represents the username for the new user
 
def initialize(username)
   #check argument username is not an empty string
   raise ArgumentError if username.empty? #keyword "raised" used instead of "throw"
 
   @username = username
end
 
#absurd method for the purpose of this article
#create 2 users
 
def User.create_bob_and_empty
   #try and create user user Bob
 
   begin #this keyword is used instead of "try"
      User.new("Bob")
   rescue ArgumentError => e
      #this rescue clause will not be performed in this example
   else
      #this clause will be performed since none of the above rescue clauses were fired
      #can be used to report success of operations where no exceptions were encountered
   end
 
   #try and create user empty
 
   begin
      User.new("Bob")
   rescue ArgumentError => e
      #this clause will be fired in this case
 
      #the variable e holds the exception that has been raised and can be used to trace exceptions stack
   ensure
      #this clause will be fired in this case
      #this is equivalent to the finally cluse in java or csharp
   end
end

So that completes the examples, now you can compare the differences between ruby and the other languages. The ruby exception hierarchy [Sieger] in comparisonm to Java and C# is rather small and limited and it is likely that you will need to create your own exceptions [Heath] by extending the hierarchy in your application. Before reading Heath’s article I should warn you his examples are poor as he uses exception for normal control-flow of logic rather than using exception for error handling which what it should only be used for.

Another thing you may also find useful is the keyword “retry”. This word/statement can be put in any rescue clause to restart the begin/end block which in lies in. This maybe useful to re-attempt to parse a file or connect to databse etc.

In ruby on top of begin/raise/rescue/end, throw/catch can also be used. Here’s an example…

1
2
3
4
5
6
7
catch(:done) do
   a = 1
   while true
      throw :done if a == 100
      a++
   end
end

In ruby, throw does not throw an exception instead it throws a string or :symbol. So as far I can tell it is only used for control-flow purposes rather than the classical case of exception handling, unless someone can tell me otherwise.

Hope you found this article useful.

Tip : Checking SQL generated by Active Record queries

Posted in Uncategorized on July 13th, 2009 by ph-lee – Be the first to comment

Those coming from PHP background or oher languages and have not had experience with object-relational mapping may at first not seem confident with the concept, still prefering to write raw SQL rather than using the tools available.

One tip I can give is to check the SQL queries that Active Record has generated and submitted to the databse. This can be done by viewing the console where the server was started (ie the command line where ruby script/server was executed). All SQL submitted to the database are displayed here when running in development mode. You can also check the logs in log/development.log, here all the text from the script/server console are stored for debugging purposes.

Hope this helps users become more confident in using Active Record rather than writing the SQL by hand.