Making Models in Rails

From Humanitarian FOSS Summer Institute 2009

Jump to: navigation, search

Once RadRails has been set-up, creating models is very simple and almost automatic.

First, we have to make sure that we have configured RadRails to our database. To do that, we have to edit the config/database.yml file and enter the database information. It should have the following format:

 adapter: mysql
 encoding: utf8
 database: database_rails
 username: dgochev
 password: PasSwOrD
 host: //where the database is located//

Once that has been done, we are ready to make models. That is done easily through the terminal on the bottom of your screen in eclipse. Let's look at an example - creating the "Incident" model, which will have the fields name and description. The terminal command for this would be as follows:

 script/generate model incident name:string description:text

We do not need to worry about making an id field, since it will be created automatically, as well as the fields created_at and updated_at. This will create a migrate file in the db/migrate folder. Then, in order for the database to be updated with this change, we have to do:

 rake db:migrate

This should have created a new table in the database called incidents, which should have the fields id, name, description, created_at and updated_at.

In more complex database structures, we may need to link several tables in our database together. For example, let's assume that in our database structure, we want multiple incidents to belong to an instance (assume that the instance model has already been created in a way similar to what we did above). This relationship can be created very easily in RadRails by simply adding another argument to the command we used above:

 script/generate model incident name:string description:text instance:references

By doing this, the field 'instance_id' will be created in the table automatically. There is one last step that needs to be taken to finish the association between the tables - to make it symmetric. This means that we need to add the line has_many :incidents to the instance model and with this the association is complete.

Sometimes the relationships between tables are even more complex; let's consider the scenario where we have a User model and a Group model, and let's say that a user can belong to many groups and a group can have many users. Thus we have a has-and-belongs-to-many relationship between these two tables. We can link the two together relatively easily by creating another model, called membership as follows:

 script/generate model membership user:references group:references

Thus it will only have two fields: group_id and user_id, and we can use it to find out all the users in a certain group as well as all the groups a user belongs to.

Once the membership model has been created, we need to utilize it in both the user model and the group model. In the user model, we will insert the following code:

 has_many :memberships
 has_many :groups, :through => :memberships

and in the group model, it will be:

 has_many :memberships
 has_many :users, :through => :memberships

We can see that the membership model provides the link between the group and user models.


RadRails vs. CakePHP : Creating Models

As we saw in the previous section, RadRails made model creation effortless and very automatic. If we were using CakePHP, on the other hand, model creation would have required manually setting up the tables in the database, creating every field one by one. That includes the id, created and modified fields. Although those fields would later be populated automatically as long as they were named according to the CakePHP convention, creating them manually would have been quite time consuming.

Another aspect of RadRails that is quite useful during development is the presence of a console, which can be accessed through the terminal by typing script/console In the console, developers can test their models to see if they are being created correctly, if the associations between them are working, etc. The console can be used like so

 >> User.all                      -- gets all the users from the database
 >> u = User.find(1)              -- creates a user object called 'u' and assigns to it the first user found in the database
 >> u.last_name = "Fox-Epstein"   -- sets the last_name variable of the object to the specified string
 >> u.save                        -- saves the object to the database

To close the console, simply type 'exit'.

Personal tools