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
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 824the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8581. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9131. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8791.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7551. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9001. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7031. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11071. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 745in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 774in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 665again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7091. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 737whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7501. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 727There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 658start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 645a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 721in integration test, you can te ...
相关推荐
Get up and running with the newly released features of Tableau 2019.2 Create enterprise-grade dashboard and reports to communicate your insights effectively Begin your Tableau journey by ...
It will present the meta-realtime layer and the newly created meta-cgl layer, its purpose, and how it can add value to poky. Table of Contents Chapter 1. Introduction Chapter 2. Cross-compiling ...
The text includes newly added and updated results on convergence, and new discussion of categorical data, numerical differentiation, and variants of the EM algorithm. It also explores the ...
The output characteristics ... The proposed PV simulator will facilitate the design aspects of PV systems and help in behaviorassessment of newly developed controllers prior to practical implementation.
When the filter_records.sql is run, it fetches all the records into a collection and further processed to separate the newly created orders and orders that are already fulfilled. The criteria used ...
2. unzip the FFT design files into the newly created project directory 3. Open the VHDL file 'fftwrap.vhd' in the Foundation HLD editor 4. In the HDL editor create a schematic macro symbol by ...
PEP 446: Newly Created File Descriptors Are Non-Inheritable Improvements to Codec Handling PEP 451: A ModuleSpec Type for the Import System Other Language Changes New Modules asyncio ensurepip ...
The book is divided into six parts, with Part I providing an introduction to the VBA language and the Excel object model. Parts II through V each consist of three to four chapters that introduce new ...
Each particle's packing location, contact-point co-ordinates, and three-dimensional graphs can be created using the packing domain given boundaries, along with numbers, and geometrical information of...
3. "With a lot of difficult problems to settle, the newly-elected president is having a hard time." 这里的"problems to settle"表示将来要解决的问题,总统因此面临困难。 4. "She sleeps with the window ...
Chapter 4 discusses how the newly defined programmable parts of the rendering pipeline interact with each other and with OpenGL's fixed functionality. This discussion includes descriptions of the ...
Provides information about programming with the High Level Shading Language for DirectX. Using HLSL, you can create C like programmable shaders for the Direct3D pipeline. Tools for DirectX Graphics...
whatever field that you would like with your newly established skill set. The skills that you will acquire from this book are highly transferrable and can be used with whatever you choose to build: ...
18. What does the speaker say about the book "Gone with the Wind"? A. It was written by a famous French author. B. It has been translated into many languages. C. It is based on a true story. 19. When...
Particle Swarm Optimization (PSO) is a newly-emerging heuristic global search algorithm based on swarm intelligence and it searches the global optimal point in the complex search space through the ...
• The tablet must work with legacy applications, and with applications written to take advan¬tage of tablet services. • The tablet driver must add minimal speed and memory overhead, so as many ...
You will also see how to set up and use CoreUpdate and Enterprise Registry, and get an introduction to the new App Container called rkt and the newly introduced cluster manager known as Kubernetes. ...
再来看一个例子:"With a lot of difficult problems to settle, the newly-elected president is having a hard time." 这里的不定式"to settle"表示的是总统即将要解决的问题,主动式表示了行动的意图和目的。...