Ruby In Rails

Ruby in Rails
Rails have many API’s and plugins to do most of its necessary functions written in Ruby.
But still a programmer need to know a lot of ruby to get his basic things done.
We all know, mostly frame works are used to manipulate databases to develop dynamic
web sites. So far a new be the most basic code need know are those which manipulates
databases. Let us do that.
Consider a table called ‘User’, having columns ‘username’ and ‘password’, with time-
stamps ‘created_at’ and updated_at’, also an autoincrement ‘id’.
if we want to retrieve all the rows in the table to a variable @users, we do.
@users = User.find(:all)
with id no: passed as a parameter to the action
@user = User.find(params[:id])
if there is a column which have row data same, to find only one we can pass the first
as : first or : last.
Usually udernames will be unique, but for explaining this condition let us assume we
have many username data same and we want to find the first one’
then we write
@first_user = User.find(:first, :conditions=>{:username=>params[:username]})
Let us try inserting SQL syntane into our query.
@last_user = User.find(:last,:conditions=>[‘username=?’,’abc’])
this retreives username = abc.
retreive, then also specify first parameter to find stataement as : first.
now we can check many other retreival statements if you are sure only one column
date that you intend to
User.find(:all,:conditions=>[‘coll=? and cpl2=?’,’abc’,’cd’])
:conditions=>[‘created at between ? and ?’,, 1.week.from_now]
similery . and , sign works well to order your retreived date.
@user= User.find(:all,:order=.”updated_at desc”)
this orders the last updated at the top and old to the bottom.
by default it is sorted in increasing order, that is latest at the bottom.
another method to retreive specified columns;
Before we could retreive some data from a bable, we have to input same rows to the
Let us try it
@user= =.’abc’)
retreives a hash model of the user table with column username set to ‘abc’.
we can set password value to ‘xyz’ by just doing
But still it is not saved on to the database table to save it do.
and it returns true if save was successful, which will only be
possible if the input date satisfies all condition specified in the tabkles
corresponding Model file
Dont fordet, if you answered all fields of a hashes from a form_tag, catch it at
the action which corresponds to the save function.
ie do
@user= User.update_attributes(params[:user])
here params[:user], user if the hash structure we retreived to @user in the
previous action which corresponded to the form-tag.
To update a table
first retreive it to a variable
@user= User.find(params[:id])
after changing the attributes in the variabls do
to update a single attribute, do

Let us count the row items in a table,

User.count(:all,:conditions =>[‘col = ?’,3])


a=User.find(:all,:conditions =>[‘col = ?’,3])


a.length or a.size
Now, we have covered some necessary database manipulating codes to start with
(this article will be updates with more about relationship between tables and
methods if using it.)

To destroy a row just do
User find(params[:id])destroy


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s