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 751the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8141. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8861. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8391.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7091. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8641. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6591. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10671. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 695in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 737in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 612again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6701. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 693whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 6941. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 683There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 616start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 617a 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 687in 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 ...
This is one of the first books on developing interactive content for games, entertainment, and simulation to cover the newly released UE4. Industry-level game concepts in level design, coding, and ...
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 ...
Design of a high bit rate burst mode clock and data recovery (BMCDR) circuit for gigabit passive optical ...developed behaviour model is verified by comparing with the detailed circuit simulation.
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 ...
They acknowledge the major contributions Fama and French have made to the literature in the past and so studied this new research with great interest. However the debate is set to continue – they ...
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 ...
Future processors will likely have large on-chip caches with a possibility of dedicating an entire die for on-chip storage in a...assumed for the newly added modules along with their validation analysis.
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...
Chapter 1, Introduction to DevOps and Continuous Delivery, deals with the background of DevOps and sets the scene for how DevOps fts in the wider world of Agile systems development. Chapter 2, A View ...
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: ...
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 ...
This book is designed for intermediate to advanced bioinformatics students who have taken a couple of introductory courses, along with the prerequisite biochemistry and computer science introductions....
• 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 ...