|Read more about this book|
(For more resources on this topic, see here.)
What is ORM?
ORM connects business objects and database tables to create a domain model where logic and data are presented in one wrapping.
In addition, the ORM classes wrap our database tables to provide a set of class-level methods that perform table-level operations. For example, we might need to find the Employee with a particular ID. This is implemented as a class method that returns the corresponding Employee object. In Ruby code, this will look like:
This code will return an employee object whose ID is 1.
Rhom is a mini Object Relational Mapper (ORM) for Rhodes. It is similar to another ORM, Active Record in Rails but with limited features. Interaction with the database is simplified, as we don’t need to worry about which database is being used by the phone. iPhone uses SQLite and Blackberry uses HSQL and SQLite depending on the device.
Now we will create a new model and see how Rhom interacts with the database.
Time for action – Creating a company model
We will create a model company. In addition to a default attribute ID that is created by Rhodes, we will have one attribute name that will store the name of the company.
Now, we will go to the application directory and run the following command:
$ rhogen model company name
which will generate the following:
We can notice the number of files generated by the Rhogen command.
Now, we will add a link on the index page so that we can browse it from our homepage.
Add a link in the index.erb file for all the phones except Blackberry. If the target phone is a Blackberry, add this link to the index.bb.erb file inside the app folder. We will have different views for Blackberry.
<a href="<%= url_for :controller => :Company %>"><span class
="title"> Company</span><span class="disclosure_indicator"/></a>
We can see from the image that a Company link is created on the homepage of our application. Now, we can build our application to add some dummy data.
You can see that we have added three companies Google, Apple, and Microsoft.
What just happened?
We just created a model company with an attribute name, made a link to access it from our homepage, and added some dummy data to it. We will add a few companies’ names because it will help us in the next section.
Associations are connections between two models, which make common operations simpler and easier for your code. So we will create an association between the Employee model and the Company model.
Time for action – Creating an association between employee and company
The relationship between an employee and a company can be defined as “An employee can be in only one company but one company may have many employees”. So now we will be adding an association between an employee and the company model. After we make entries for the company in the company model, we would be able to see the company select box populated in the employee form.
The relationship between the two models is defined in the employee.rb file as:
belongs_to :company_id, 'Company'
Here, Company corresponds to the model name and company_id corresponds to the foreign key.
Since at present we have the company field instead of company_id in the employee model, we will rename company to company_id.
To retrieve all the companies, which are stored in the Company model, we need to add this line in the new action of the employee_controller:
@companies = Company.find(:all)
The find command is provided by Rhom, which is used to form a query and retrieve results from the database. Company.find(:all) will return all the values stored in the Company model in the form of an array of objects.
Now, we will edit the new.erb and edit.erb files present inside the Employee folder.
<% @companies.each do |company|%>
<option value="<%= company.object%>"
<%= "selected" if company.object == @employee.
If you observe in the code, we have created a select box for selecting a company. Here we have defined a variable @companies that is an array of objects. And in each object we have two fields named company name and its ID. We have created a loop and shown all the companies that are there in the @companies object.
In the above image the companies are populated in the select box, which we added before and it is displayed in the employee form.
What just happened?
We just created an association between the employee and company model and used this association to populate the company select box present in the employee form.
As of now, Rhom has fewer features then other ORMs like Active Record. As of now there is very little support for database associations.