6.1 play with the newly created model

6.1 play with the newly created model.

in prev chapter, we created a user model, now we will play with it.

 

1. first, let's start rails console,

 

this time we will use a param: --sandbox


this param make sure when you exit the console, all changes made here will be rolled back!!!

 

rails console --sandbox

 

(if you want to play with test database, you can

 

rails console test

 

2. I have a very good habit that when play with the console, I will keep a log terminal open, to see what is happening on the log.

 

use this command to power up the log terminal:

 

tail -f log/development.log


-f param will make sure it will add newly added lines to the terminal when new lines created.

 

3. after you start rails console, Rails will auto load rails environment, so models and controllers are all loaded.

you can use them directly

 

4. for a model that inherit from active record, you can initialize it using a hash that include the value of the attributs.

 

for example:

user = User.new(:name => "abcd", :email => "abcd@abcd.com")

 

to save this record, you need to call:

 

user.save

 

this line will save the user info into database.

 

this method return true if save successful, return false if the save fails.

 

5. user = User.new

user.save

 

these two lines can create and save a user record.

 

there is anoter way that will create and save by one line of code:

 

user = User.create(:name => "abcd", :email => "abcd@abcd.com")

note: User.create will return the object itself.

 

6. the opposite side of User.create is user_object.destroy

 

user.destroy

 

this method will also return the destroied user object.

(but in general, I have never find this returned destroied object of any use. this object is still in memory after destroy)

 

 

7. next we will learn "find" method that will be used everyday:

 

a. User.find(1) =======> find by user id

 

b.  a more genral use of find is finding by a colum:

 

User.find_by_email "abcd@abcd.com"

(but, will the effieciency very slow to search through database by find by email? you are right, we will solve it by adding index to database later!!!)

 

c. User.first, this is another find.

 

d. User.all, this will return an array.

 

 

8.  next we will learn how to update user object.

 

a. user = User.find_by_email "abcd@abcd.com"

user.name = "abcd2"

user.save

 

note, "user.save" is need to save it into database!!!

 

without it, it will not get saved, we can see this fact by using user.reload:

 

user = User.find_by_email "abcd@abcd.com"

user.name = "abcd3"

user.reload.name   ====> "abcd", 

 

 

b. another way of updating user object is using a method called, update_attributes

 

user.update_attributes(:name => "def", :email  => "ghz@ghz.com" )

 

will return true if update success

 

this method will accept a hash as param, using the hash to update records.

 

one thing to note:  if you used to define any attr_accessible attributes, then only columns that belong to attr_accessible can be updated using this method