Beginning Ruby on Rails

Hello, My name is Tyler DeLawder I am currently learning Ruby on Rails a friend of my Richard Ramsden showed me this site called railsforzombies.org I went on the site and registered and began the program.

You can check out Rick's blog here http://rramsden.ca/

Here are some examples of what I have learned so far in the program.

idNameAge
1Tyler19
2Rick24

Find Name where id = 1 and store it in a variable

So here is what I done.

> n = Name.find(1)

This returns

 

=> #<id: 1, name: "Tyler", age: 19">

 

Now notice I stored the variable as "n".

Now if you wanted to get something from the database, Let's say my Age we would do this.

 

> puts n[:Age]

We would get this.

 

=> 19

You can also write it like this in rails

 

>  puts n.Age

Now if you wanted to add to the database you would do.

 

Now note the Rails takes care of the id in the new database.

> t = Name.new

t.Name = "Yomi"

t.Age = "18"

t.save

To Update an existing item in the database you would do.

 

 

t = Name.find(2)

t.Name = "Steven"

t.save

 

And to delete an item in the database you would do.

 

 

t = Name.find(3)

t.destroy

We can also do this.

 

Name.find(3).destroy

 

And to delete all you can do

 

Name.destroy_all

 

We can also update the attributes by doing this.

 

 

t = Name.find(1)

t.update_attributes(

:Age => "18"

)

Lets look at our results.

idNameAge
1Tyler18
2Steven24
3Yomi18

Blog Archive

Tweet it!

RSS

Visitors

8679

Free Driver Update Software