Posts Tagged ‘how-to’

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.

How to : Use Ruby on Rails ActiveRecord to query multiple tables with joins and conditions

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

This is a back-to-basics article I decided to revisit. Here we’ll demonstrate how to query two related tables using ActiveRecord with joins and conditions rather than having to write the SQL by hand. Instead we let Activerecord do all the hard work. I like to learn by example so let’s set the scene.

We’re an estate agent and have an application which stores client details and property details. There are many clients and many properties, a client can have many properties but a property can only belong to one client. A simple one-many relationship. Here are some quick models.

models/client.rb

1
2
3
class Client < ActiveRecord::Base
   has_many :properties
end

models/property.rb

1
2
3
class Property < ActiveRecord::Base
   belongs_to :client
end

Heres some information the above two tables may contain for the purpose of this example…

Client table structure

  • id (primary key)
  • first_name
  • last_name

Property table structure

  • id (primary key)
  • client_id (foreign key)
  • house_number
  • address_first_line

#Note that client_id is needed to keep a relationship between the two tables. This foreign key is handled by rails ActiveRecord. Since we specified the relationship in our models it will use client_id to look up primary keys in the client table (ie. client.id).

controllers/home.rb

1
2
3
4
5
class HomeController < ApplicationController
   def my_very_specific_query
      my_client = Client.find(:first, :joins => [:properties], :conditions => {:first_name => "Fred", :properties => {:house_number => 3}})
   end
end

The above is very specific query which suits the purpose of this example but rather useless in the real world. It finds the first client in the database (sort by id in ascending order by default) where their first name is “Fred” AND owns a property with the house number three.

:joins performs a INNER JOIN, (however if you need you can overide this by writing the exact join by specifying the SQL). This will let you specify conditions for your query which regard related tables.

:conditions lets you specifying the query conditions that will return when true. We have used a hash in this case specifying conditions from the model/table we wish to return (in this case Client) and condition in a related table (Property). Of course extra joins and conditions can be added to suit the need of your query. For further info please see the API on ActiveRecord

You should now be able to use ActiveRecord to query multiple tables using :joins and :conditions

Hope you found this useful.

How-to : Deploy Ruby on Rails in a subdirectory and not in root

Posted in Uncategorized on February 27th, 2009 by ph-lee – 1 Comment

Normally one would deploy their rails application in the root directory (top-level) ie where the domain is, for example…

www.your-domain.com

However, you may want to deploy your app in a subdirectory such as…

www.your-domain.com/your_app

You may want to do this if you want to deploy several applications on the same domain or temporarily for testing purposes. So heres what to do. Naviagate to the environment.rb file found here…

root | configuration | environment.rb

Open the file in your favourite text editor. Add the relative url configuration to your config as shown below. I added it to the end of the do statement.

Rails::Initializer.run do |config|
config.action_controller.relative_url_root = "/your_app"
end

This setups the environment for rails to now run with url root of your_app.

Happy deploying!