Archive

Posts Tagged ‘postgres’

Preparing Ruby on Rails environment – firstapp

February 1st, 2009 2 comments

Now as we have all the environment prepared we can start with example application to check if everything is working.

For Begining we can check if postgres is working:

/etc/init.d/postgresql-8.3 status

If You did not added it to autostart it’s probably stopped, so You can start it with:

/etc/init.d/postgresql-8.3 start

And if it is not a big problem to have it running all the time add it to autostart:

rc-update add postgresql-8.3 default

Additional is needed as my previous instructions require additional step – adding postgresql support in ruby:

gem install postgres

Referring to my friend Andrzej instructions it should be ruby-pg, I’m not sure which one is better.

Following steps are very similar to instructions at netbeans docs so You can open it in separate window to have an overview of the GUI side.

Open netbeans from the console (in most distros netbeans will be in Yours applications menu and may be available on desktop):

/path_to_netbeans-6.5/bin/netbeans

Right click the empty area within Projects tab and choose “New Project”, there choose “Ruby” -> “Ruby on Rails Application” and click “Next >”. On the next screen You cna change nam of the application, You do not have to change all the params there, just click “Next >”. Following screen is “Database Configuration”, here You have to change the “Database Adapter” to “postgresql” enter Yours user name in the field “User Name” and press “Finish”.

That is Yours first application, now we will use famous functionality of rails – scaffold, right click the new project and select “Generate…”. From the “Generator:” list select scaffold, enter “Person” in “Model Name:”, enter “name:string” in “Attribute Pairs (field:type):” and press “OK”.

Worth explanation is feature of rails called pluralization, You can see in Models, that there is person.rb model, but in Controllers You have people_controller.rb controller, this is because rails calls “person”.pluralize before applying the name for controller or view. You can check this by navigating to the project “cd ~/NetBeansProjects/RailsApplication1″ and entering “script/console”, here You can play with rails and Yours application.

Now after we have automaticly generated code of application we can continue with creating database for the application. Right click Your application, choose “Run/Debug Rake Task”, now choose “db:create” from the list and click the “Run” button. Ther will be no preaty message “Database created successfully”, but if You did not got any error message in few seconds it means everything went fine. Next step is to migrate database to current state – right click project, select “Migrate Database” -> “To Current Version”. In the output You should find somthing like this:

(in /home/mpapis/NetBeansProjects/RailsApplication1)
NOTICE:  CREATE TABLE will create implicit sequence “people_id_seq” for serial column “people.id”
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index “people_pkey” for table “people”
==  CreatePeople: migrating ===================================================
— create_table(:people)
-> 0.0209s
==  CreatePeople: migrated (0.0210s) ==========================================

The database is prepared for our application, now we have to change few default settings to make person the most important in our application. Please open Configuration / routes.rb file by double clicking it. Start search by CTRL+F in the Find box enter “map.root”, uncomment the found line and change “welcome” to “people”. Last step is to rename the auto generated index.html file in the public folder, right click the file, chose rename and add suffix “_old” to the displayed index in the edit box.

Thats all, click the green triangle in the toolbox on the top of netbeans. This should open Your browser with the new app, You can check it by navigating through it.

Small hint for Firefox users, there might be needed change in netbeans to open correctly – please anvigate to Menu “Tools” -> “Options” choose “Firefox” in the “Web Browser” list and click “Edit” button. Here replace “Arguments” edit box content with:

“{URL}”

In the next article we will start our application through apache passanger instead of using rails built in WEBrick.

Categories: Development, Linux Tags: , , ,

Preparing Ruby on Rails environment – postgresql

January 20th, 2009 Comments off

Time to start some development, for next project I have decided to use (and learn) Ruby on Rails (RoR).

Many persons use mysql to develop rails apps, as it is easy to setup and use.

But postgresql has more futures then mysql, one of them is known to be used by current RoR, It is transactional DDL update, in Rails world it is called transactionall migrations.

So this is what I had to do to get it working on my gentoo box:

emerge postgresql-server

emerge postgresql-server –config

/etc/init.d/postgresql-8.3 start

su – postgres -c createuser mpapis

# or you could use: sudo -u postgres createuser mpapis

In the listing i have used settings from my computer, /etc/init.d/postgresql-8.3 can be any other name starting with postgres, replace mpapis with your user name.

After running those commands You are able to run psql from yours account, from now on You can run following commands to menage Yours postgresql database:

  1. createdb DATABASE_NAME
  2. dropdb DATABASE_NAME
  3. createuser USER_NAME
  4. dropuser USER_NAME
  5. pg_dump DATABASE_NAME > dump.sql
  6. pg_dumpall > dump.sql
  7. psql -d DATABASE_NAME

This knowledge should be enough to continue with rails, but this comes in next days.

Categories: Development, Linux Tags: , ,